【发布时间】:2017-10-17 21:56:24
【问题描述】:
我正在阅读这个documentation on Vue components,但我的组件属性使用了 Vuex 数据。
从示例中,如果country_id 在data 方法中,它可以正常工作。但是当country_id 是从Vuex 存储返回数据的计算属性时,子组件的internalValue 总是初始化为undefined。
我做错了什么?
父组件:
export default {
computed: {
country_id() {
return this.$store.state.user.country_id
}
},
mounted: function () {
this.$store.dispatch('user/load');
}
}
<template>
<child v-model="country_id"></child>
</template>
子组件:
export default {
props: [ 'value' ],
data: function() {
return {
internalValue: null,
};
},
mounted: function() {
this.internalValue = this.value;
},
watch: {
'internalValue': function() {
this.$emit('input', this.internalValue);
}
}
};
<template>
<p>Value:{{value}}</p>
<p>InternalValue:{{internalValue}}</p>
</template>
【问题讨论】:
标签: javascript vue.js vuejs2 vuex