search

Home  >  Q&A  >  body text

javascript - Vue2 Ajax (axios) failed to update dom data via paging

Since in the project, the background data is sent to the front end at one time, the front end needs to do some paging processing.
Vue2 Axios is used to make ajax requests. Currently, the backend data console.log can be printed successfully, but it cannot be updated on the dom.

html

 <section class="main">
            <ul class="list">
                <li v-for="info in listt2">
                    <img src="#" v-bind:alt="info.Name">                                                     
                    <h4> <a class="talk" target="_blank" v-bind:href="'content.html?'+info.id">{{ info.title }}</a></h4>
                    <span class="ckey">【{{ info.key }}】 </span> <span style="color: #ffffff;"> {{info.id}}</span>
                </li>
            </ul>
             <!--分页按钮区域-->
            <p class="pages" v-show="onn">   
                <button class="previem" @click="page('last')" v-show='curPage>0'>上一页</button>
                <button class="next" @click="page('!last')" v-show="curPage<pageCount-1">下一页</button>
            </p>
        </section>

JS

  Vue.prototype.$ajax = axios;  //修改原型链 
    var vm = new Vue({
        el: '.main',
        data: {
            listt2:[ ],  //页面要展示的数据
            pageSize:10,  //翻页每页显示数据
            curPage:0,  //当前页面
            pageCount:'',  //总共页面数
            onn:true,  //默认显示分页
            items:' ', //后台数据         
        },
        created:function(){
            //Ajax获取后台数据,获取的数据存储在 this.items
            var url = "api.json";
            this.$ajax.get(url)
                .then(function (response) {
                    var jsons = response.data.getJson;
                    var self = this;
                    this.items =jsons;
                    console.log(self.items);

                }).catch(function (error) {
                    console.log(error);
                });
            this.fanye();  //调用分页
        },
        methods: {
            page: function (el) {    //点击翻页
                el == 'last' ? this.curPage-- : this.curPage++;
                var curtotal = this.curPage * this.pageSize;
                var tiaoshu = this.curPage * this.pageSize + this.pageSize;
                this.listt2 = this.items.slice(curtotal,tiaoshu);
                document.body.scrollTop = 0;
            },
            fanye: function () {      //分页处理
                var _this = this;
                _this.listt2 = [];
                if (_this.items) {
                    _this.pageCount = Math.ceil(_this.items.length / _this.pageSize);
                    for (var i = 0; i < _this.pageSize; i++) {
                        if (_this.items[i]) {
                            _this.listt2.push(_this.items[i]);
                        }
                    }
                }
            }
        }
        })

Returned simulation data format

{
    "getJson":[
        {
            "id":"59",
            "key":"science",
            "title":" 动物也是科技宅,这些智能科技装备你想要吗? ",
            "time":"2017-05-12",
            "name":"两个质子",
            "eng":"lianggezhizi"
        },
        {
            "id":"60",
            "key":"science",
            "title":" 肯定你没见过的养老新科技! ",
            "time":"2017-06-19",
            "name":"老年健康生活方式",
            "eng":"aged-expo"
        }]
}


I have checked it several times, but there is still only a style but no data. I would like some advice from an expert.

PHP中文网PHP中文网2716 days ago1470

reply all(2)I'll reply

  • 習慣沉默

    習慣沉默2017-06-24 09:46:12

    In the first then requested in the created method, mention var self = this; to this.$ajax.get(url) above.
    Scope issue, this in the then method is no longer this in vue

    reply
    0
  • 欧阳克

    欧阳克2017-06-24 09:46:12

    You created ajax data acquisition is asynchronous. When you this.fanye() is executed, no data is passed in at all; you can interrupt the point, console.logdata, try it first

    reply
    0
  • Cancelreply