javascript - 为所有人更改 Vuejs 中的数据

标签 javascript jquery ajax vue.js

在ajaxtotalUserPosts返回零之后我遇到了这个问题,我不想要这个!

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,请帮助我

最佳答案

您在 ajax 函数返回之前调用滚动。您应该在 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();
        }
    })

关于javascript - 为所有人更改 Vuejs 中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36861221/

相关文章:

javascript - AJAX post方法,有时在页面加载时触发,有时不触发

javascript - Meteor:根据现有集合的一个属性创建一个新集合

javascript - 为什么选择范围和弹出窗口不能在 javascript 中一起工作?

javascript - jQuery 多个选择器相同的功能

javascript - 如何使用 jQuery 更改创建的 D3 对象的 CSS?

javascript 渲染的 collection_select 输出 HTML 选项标签

javascript - 使用 Javascript + Ajax 验证和提交表单

javascript - 添加图像,同时从另一个 UL 动态地将元素添加到无序列表

javascript - 全局点击事件阻止元素的点击事件

javascript - HighCharts pointPlacement 选项在单柱形图中不起作用