改变所有人的Vuejs数据

时间:2016-04-26 09:46:41

标签: javascript jquery ajax vue.js

我在ajax totalUserPosts返回零后遇到问题零,我不想要这个!

new Vue({
el: "#app",
data: {
  totalUserPosts:0,
},
methods:{
  getPostsAjax () {
    $.ajax({
      url:url,
      type: 'get',
      cache: false,
      dataType: "json",
      data:{
        limit: this.limit,
        id: this.vue_profile_id
      }
   }).done((data) => {
     this.userPosts = data.posts;
     this.totalUserPosts = data.total
   }.bind(this))
 },
 getUserPosts () {
   this.getPostsAjax()
   $(window).scroll(() => {
     if($(window).scrollTop() + $(window).height() > $(document).height() - 150) {
       console.log(this.totalUserPosts)
     }
   })
 }
})

我想在完成ajax请求之后更改totalUserPosts请帮助我

1 个答案:

答案 0 :(得分:1)

你在ajax函数返回之前调用scroll。您应该在ajax请求的回调函数中包含滚动功能,可能是这样的:

    new Vue({
    el: "#app",
    data: {
      totalUserPosts:0,
    },
    methods:{
      getPostsAjax () {
        $.ajax({
          url:url,
          type: 'get',
          cache: false,
          dataType: "json",
          data:{
            limit: this.limit,
            id: this.vue_profile_id
          }
         }).done(this.getUserPosts)
        },
         getUserPosts (data) {
           this.userPosts = data.posts
           this.totalUserPosts = data.total
           $(window).scroll(() => {
           if($(window).scrollTop() + $(window).height() > $(document).height() - 150) {
             console.log(this.totalUserPosts)
           }
          })
         }
        },
        ready () {
         this.getPostsAjax();
        }
    })