简体   繁体   English

vue js ajax调用无法读取未定义的属性“ push”

[英]vue js ajax call cannot read property 'push' of undefined

I am having problems with vue.js when it comes to pushing data from the database to the webpage. 在将数据从数据库推送到网页时,vue.js出现问题。 I am using laravel 5.3 which converts data to Jason. 我正在使用laravel 5.3将数据转换为杰森。 the code posts to the server the number of records to be fetched then the server response with the data in which i want to display on the webpage. 该代码将要获取的记录数发送到服务器,然后服务器响应我要在网页上显示的数据。 here is Vue js Code 这是Vue js代码

new Vue({
        el: "#projects",
        data: {

            count: 0,
            fetched_projects: []
}

 methods:{
       checkproject: function(){
            var fetch = this.count += 4;               

           axios.post('/loadmore', {fetch: fetch })
               .then(function(response) {

                  this.fetched_projects.push(response.data);

               })
               .catch(function(error){
                   console.log(error);
               });

       }

HTML HTML

 <div class="row">
    <div class="col-md-3">
        <ul v-for="more_projects in fetched_projects">
            <li>@{{ more_projects }}</li>

        </ul>

    </div>
</div>

Laravel controller Laravel控制器

 public function setloadMore(Request $request){

  $new_projects = $request->fetch;

  $results = Model1::with('relation')->take($new_projects)->get();

  return $results;


}

The problem is on this.fetched_projects.push(response.data); 问题出在this.fetched_projects.push(response.data); its giving me "cannot read property 'push' of undifined" 它给了我“无法读取未定义的属性'push'”

You'd want this to be your component, so use an arrow function: 您希望this成为您的组件,因此请使用箭头功能:

 methods:{
   checkproject: function(){
        var fetch = this.count += 4;               

       axios.post('/loadmore', {fetch: fetch })
           .then(response => {
              this.fetched_projects.push(response.data);
           })
           .catch(function(error){
               console.log(error);
           });

   }

您遇到“这个”问题,请尝试在定义了fetch的地方定义类似var的东西=,并在帖子应该使用的地方使用它代替它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM