【问题标题】:Data cannot be found in promise axios.all在promise axios.all 中找不到数据
【发布时间】:2019-07-20 15:03:48
【问题描述】:

问题是(this.form.tasks没有其他数据)在promise then()中找不到 与 axios.all。 在承诺之外它有效,但在内部无效。我得到错误:

Uncaught (in promise) TypeError: Cannot read property 'form' of null

        axios.all([
            axios.get('/tasks/'+this.token_proj),
            axios.get('/keywords/'+this.token_proj),
            axios.get('/getTasks/'+this.token_proj),
            axios.get(`/discussion/${this.token_proj}/${this.form.page}`)
        ])
        .then(axios.spread(function (tasksResponse, keywordsResponse,getTasksResponse,discResponse) {

            console.log('tasks', tasksResponse.data);
            console.log('keyword', keywordsResponse.data);
            console.log('getrask', getTasksResponse.data);
            console.log('disc', discResponse.data);

            this.form.tasks = tasksResponse.data;



        }));

tasks 变量在 data 中设置并存在。我使用 Vuejs 和 axios vform。

data(){
        return{
            form: new Form({

            tasks:[],
            tasks_management:[],
            discuss:{},
            users:[],
            keywords:[],


            })
        }
    }

【问题讨论】:

  • 在您的 axios 调用之前创建一个变量 const vm = this 并在您的回调函数 vm.form.tasks = tasksResponse.data 中使用它。
  • 它工作得很好,谢谢@HusamIbrahim!

标签: php laravel vue.js


【解决方案1】:

考虑使用fat arrow function 在匿名回调中保留this 的范围,而不是别名this

.then(axios.spread((tasksResponse, keywordsResponse,getTasksResponse,discResponse) => {
  //this is now scoped correctly.
}))

【讨论】:

    猜你喜欢
    • 2017-10-31
    • 1970-01-01
    • 1970-01-01
    • 2017-02-13
    • 1970-01-01
    • 2021-06-24
    • 2018-04-11
    • 2016-10-03
    • 2019-02-23
    相关资源
    最近更新 更多