我有像下面这样的 Prop
props: ['applicants'],
我想使用像下面这样的 Prop
methods : {
fetchapplicants() {
let self = this;
console.log(self.applicants.length);
}
},
mounted() {
this.fetchapplicants();
}
我得到输出
undefined
最佳答案
首先你不需要使用self
从我看到的代码来看,没有必要。所以,就这样做吧。
methods : {
fetchapplicants() {
console.log(this.applicants.length);
}
},
mounted() {
this.fetchapplicants();
}
另外,确保将 props 作为对象传递,这是推荐的方式,它将帮助您定义传递的 props 的类型和默认值
props: {
applicants: {
type: Array,
required: false,
default: []
}
}
最后,确保将数组传递给组件。我认为这可能是问题所在,因此在您的代码中的任何地方,当您调用组件时,请确保甚至通过将默认数组传递为以下内容来测试它:
<my-component :applicants="[1, 2, 3, 4, 5, 6, 7]"></my-component>
关于vuejs2 - 在 vue.js 中的mounted() 中使用PROPS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48850668/