javascript - vue js ajax调用无法读取未定义的属性 'push'

标签 javascript php laravel-5.3 vuejs2 axios

在将数据从数据库推送到网页时,我在使用 vue.js 时遇到了问题。我正在使用将数据转换为 Jason 的 laravel 5.3。代码将要获取的记录数发布到服务器,然后服务器响应我要在网页上显示的数据。这是 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

 <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

 public function setloadMore(Request $request){

  $new_projects = $request->fetch;

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

  return $results;


}

问题出在 this.fetched_projects.push(response.data); 上,它给我“无法读取未定义的属性‘push’”

最佳答案

你希望 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);
           });

   }

关于javascript - vue js ajax调用无法读取未定义的属性 'push',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43070412/

相关文章:

javascript - 如何将内部函数值作为参数传递给其回调

javascript - 使用字符串组合生成排名

javascript - jquery mobile listview 刷新相同列表丢失样式

php - 拉拉维尔 5.3 : Many to many realtionship's data

laravel - Laravel Blade View 中的变量未定义错误

java - YUI 压缩器在构建时压缩和替换

php - laravel sql 语句 - 如何向 isnot 添加附加子句 - 'where' '!=' 或

php - 数据库提交时不更新值

php - PDO mysql : How to know if insert was successful

php - 为什么我在尝试使用自定义用户提供程序登录时收到此 "Class App\Listeners\LogAuthenticationAttempt does not exist"错误?