1. 程式人生 > >vue 簡單模擬 後臺資料渲染頁面

vue 簡單模擬 後臺資料渲染頁面

第一步

注:首先html,css已經寫好!!!並且本例是以html 結尾的,使用vue獲取本地檔案。

1.新增vue.js

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>

2.使用 axios  來獲取資料:

<script src="https://unpkg.com/axios/dist/axios.min.js"></script>

3.html和css(vue例項)


<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title></title>
    </head>
  <body>
    <div id="app">
          <div class="student" style="width: 80%;margin: 0 auto;">
              <ul style="list-style: none; text-decoration: none;"> 
                  <li v-for="person in persons" style="width: 100%; height: 30px; background: pink;border-bottom: 1px solid #ccc;">
                      <a href="">
                          <div style="float: left;">
                              {{person.name}}
                          </div>
                          <div style="float: right;">
                              {{person.message}}
                          </div>
                      </a>
                  </li>
              </ul>
          </div>
    </div>
        <script src="https://cdn.jsdelivr.net/npm/

[email protected]/dist/vue.js"></script>
        <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
        <script type="text/javascript">
            var vm = new Vue({
                el:"#app",//例項
                data: {//資料M
                    persons:[],
                },
                created:function() {//掛載
                    var self = this;
                    self.initdata()
                },
                methods:{//定義函式
                    initdata:function() {
                        var url = "one.json";//定義引入地址
                        _this = this;//改變this指向
                        axios.get(url).then(function(result) {//獲取資料
                            console.log(result.data);
                            _this.persons = result.data.persons;
                        })
                    }
                }
            })
        </script>
</body>
</html>

4.本地.json資料

 
{
    "persons":[
        {
            "name":"li","message":"知道了","time":"01:00","url":"images/student/girl.png"
        },
        {
            "name":"ja","message":"在嗎","time":"02:00","url":"images/student/girl.png"
        },
        {
            "name":"he","message":"真的","time":"03:00","url":"images/student/girl.png"
        },
        {
            "name":"lu","message":"好的","time":"04:00","url":"images/student/girl.png"
        },
        {
            "name":"xiao","message":"載入","time":"05:00","url":"images/student/girl.png"
        }                
    ]
}

注意:請開啟服務或者用hbulider黏貼程式碼段!!!