Home  >  Article  >  Web Front-end  >  How to use Vue.js with ajax to bind data

How to use Vue.js with ajax to bind data

不言
不言Original
2018-07-11 17:42:571982browse

This article mainly introduces the method of binding data with Vue.js and ajax. It has certain reference value. Now I share it with you. Friends in need can refer to it

        <script>
            new Vue({
                el: &#39;#vue-menu3&#39;,      //p的id
                data: {                    : ""    //数据,名称自定
                },
                created: function () { //created方法,页面初始调用    
                    var url = "GetData.ashx";
                    this.$http.get(url).then(function (data) {   //ajax请求封装
                        var json = data.body;
                        var jsonObj = eval(&#39;(&#39; + json + &#39;)&#39;);
                       // console.info(jsonobj);
                        this.all = jsonObj;
                    }, function (response) {     //返回失败方法调用,暂不处理
                        console.info(response);
                    })
                }
            });
        </script>

Page code

        <p id="vue-menu3">
            <table class="table table-striped" >
                <caption>借阅书籍列表</caption>
                <thead>
                    <tr>
                        <th>书籍编号{{all.id}}</th>
                        <th>书名</th>
                        <th>管理人员</th>
                        <th>借阅时期</th>
                        <th>归还时间</th>
                    </tr>
                </thead>
                <tbody>
                    <tr v-for="value in all.libraryBooks">
                        <td>{{value.bookId}}</td>
                        <td>{{value.name}}</td>
                        <td>{{value.chargePerson}}</td>
                        <td>{{value.borrowTime}}</td>
                        <td>{{value.returnTime}}</td>
                    </tr>
                </tbody>
            </table>
        </p>

The following is data binding separately

      new Vue({
                el: &#39;#vue-menu3&#39;,      //p的id
                data: {                    libraryInfo: ""    //数据,名称自定
                },
                created: function () { //created方法,页面初始调用    
                    var url = "GetData.ashx";
                    this.$http.get(url).then(function (data) {   //ajax请求封装
                        var json = data.body;
                        var jsonobj = eval(&#39;(&#39;+json+&#39;)&#39;);
                        console.info(jsonobj);
        
                        //我的json数据参考下面
                        this.libraryInfo = jsonobj.libraryBooks;
                    }, function (response) {     //返回失败方法调用,暂不处理
                        console.info(response);
                    })
                }
            });

Page code

   <p id="vue-menu3">
            <table class="table table-striped">
                <caption>借阅书籍列表</caption>
                <thead>
                    <tr>
                        <th>书籍编号</th>
                        <th>书名</th>
                        <th>管理人员</th>
                        <th>借阅时期</th>
                        <th>归还时间</th>
                    </tr>
                </thead>
                <tbody>
                    <tr v-for="value in libraryInfo">
                        <td>{{value.bookId}}</td>
                        <td>{{value.name}}</td>
                        <td>{{value.chargePerson}}</td>
                        <td>{{value.borrowTime}}</td>
                        <td>{{value.returnTime}}</td>
                    </tr>
                </tbody>
            </table>
        </p>

  Must reference vue-resource.js and vue.min.js

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

Vue Introduction to string templates

How to implement a stack in JavaScript

The above is the detailed content of How to use Vue.js with ajax to bind data. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn