Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在Vuejs中更改所有_Javascript_Jquery_Ajax_Vue.js - Fatal编程技术网

Javascript 在Vuejs中更改所有

Javascript 在Vuejs中更改所有,javascript,jquery,ajax,vue.js,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.lim

在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请在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();
        }
    })

你能把console.log(this.totalUserPosts)放在这里吗;分配完后,在done函数内,告诉我们输出是什么?totalUserPosts=11的返回值这不是返回零您是否尝试声明
$(窗口)。在done函数内滚动
函数?有两个问题:1)
getPostsAjax()
是一个异步方法,因此
scroll()中的代码
之后,它将在Ajax调用完成之前运行。我的建议是:使用承诺或实现回调,这样您就可以在回调中执行
getPostsAjax(cbFunctionHere)
2)到
。scroll()
将不会指向vue实例。你应该使用一个正常的函数并将其正确绑定。否:(这为我返回undefined:(你有另一个解决方案??什么是undefined?因为这就是我在上面评论的目的。编辑:这个解决方案的问题是“this”因为我们在滚动事件上下文中,所以有另一个上下文。@杰夫:您能调整您的解决方案以使用一个变量来代替此变量吗?es16 arrow函数保留了写入它们的范围@TehSphinX