【发布时间】:2020-10-18 12:02:12
【问题描述】:
我调用 api 来获取数据,我用 postman 测试它,laravel 将它作为一个数组正确发送,但是 vue 把我的数组变成 [ob: Observer] 我不能感谢@Stockafisso,从中提取我的数组已经解决了,但是
编辑:现在我的问题是,programming_languages 数组只能在 getLanguages() 方法中访问,其他任何地方都无法访问
data(){
return{
answer: '',
maxWrong: '',
programming_languages : []
}
},
methods:{
getLanguages(){
axios.get('/api/toController').then((response)=> {
this.programming_languages = JSON.parse(JSON.stringify(response.data)); //works fine
this.answer = this.programming_languages[Math.floor(Math.random() * this.programming_languages.length)].name; //works fine
this.maxWrong = this.answer.length;// works fine here, i dont want to initiaize answer variable in this method
});
},
randWord(){
this.answer = this.programming_languages[Math.floor(Math.random() * this.programming_languages.length)].name;// it is not working here
//Error in created hook: "TypeError: Cannot read property 'name' of undefined"
this.maxWrong = this.answer.length;// doesn't work here
}
},
created(){
this.getLanguages();
this.randWord();
}
我能做什么? 谢谢
【问题讨论】:
-
你能告诉我们api响应吗?
-
返回模型名::all();它发送一个嵌套数组,我通过邮递员对其进行了测试,这是正确的,当我使用 JSON.parse(JSON.stringify(response.data) 时,我还可以在 [ob: Observer] 中看到该数组,但我无法提取它进入数组
标签: laravel vue.js vue-reactivity