【发布时间】:2020-10-09 06:16:31
【问题描述】:
我试过了:
get_choices: function (item, index){
console.log(item)
},
this.questions.forEach(this.get_choices())
但它说
Error in mounted hook: "TypeError: this.questions.forEach is not a function"
我也试过了:
for(question in this.questions){
console.log(question)
}
但它没有输出任何东西,也没有显示任何错误。 有什么帮助吗?非常感谢!
更新:
这是this.questions。它是一个对象数组:
data: {
questions: [
{'name': 'etc', 'content': 'etc'},
{'name': 'etc2', 'content': 'etc2'},
],
},
【问题讨论】:
-
什么是
this.questions?它似乎不是一个数组或任何可访问的东西。 -
你应该传递函数而不是调用它:
this.questions.forEach(this.get_choices) -
@HaoWu
forEach未被识别为存在于this.questions上的方法。传递函数引用无助于解决这个问题。 -
@HaoWu 是的,我试过了,但同样的错误。
-
@VLAZ 你是对的,最重要的是......确保
this.questions是一个数组,然后传递我提到的函数
标签: javascript vue.js