vuejs2 - 在 vue.js 中的mounted() 中使用PROPS

标签 vuejs2

我有像下面这样的 Prop

props: ['applicants'],

我想使用像下面这样的 Prop
methods : {
fetchapplicants() {
                let self = this;
                console.log(self.applicants.length);
            }
        },
mounted() {
     this.fetchapplicants();
}

我得到输出 undefined
enter image description here

最佳答案

首先你不需要使用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/

相关文章:

javascript - Vue2 事件总线在第二次调用后工作

vuejs2 - 验证日期输入字段

javascript - V-model 没有监听输入值的变化(vuejs)

javascript - 加载图像时的触发方法(Internet Explorer)

javascript - 测试从 Vue JS 应用程序中的 API 获取数据的代码

javascript - 为什么我的图像不能在 Vue.js 2 中加载?

javascript - 如何在vuejs2中使用节点包vue-tweet-embed?

laravel-5 - 在单个 vue 组件中包含本地 CSS 文件

javascript - 将数据从包含槽内容的组件传递到该槽内容组件

html - 全日历高度不考虑周围容器