【发布时间】:2020-01-13 01:42:34
【问题描述】:
目前,我正在使用 Vuex 来管理应用程序级状态。
我只需要在 Vue 组件生命周期内存储数据。是否可以在不涉及全局存储的情况下使用 Vuex 来存储本地组件状态?
例如,我有一个这样的组件:
class SomeComponent extends Vue {
id = this.$props.componentId;
localData = {};
async created() {
this.localData = await apiClient.getDataById(this.id);
}
}
然后我可以使用具有不同 prop componentId 的组件,并且每个组件都应该管理自己的状态。
<SomeComponent componentId="1" />
<SomeComponent componentId="2" />
<SomeComponent componentId="3" />
我希望看到如此理想的变体:
import LocalStore from './local-store';
class SomeComponent extends Vue {
id = this.$props.componentId;
localStore = new LocalStore(); // <- this is the Vuex store
created() {
localStore.getDataById(this.id);
}
}
【问题讨论】: