如何在vue的onMounted方法里面使用vuex getter?
How to use vuex getter inside the onMounted method in vue?
我从 vuex 中存储的数据库中获取数据。我在设置方法中使用 getter 检索它,但我想在呈现页面之前使用其中的一些数据,最好是在 onMounted 方法中。我不知道如何使用 Compositions API 来做到这一点。这是我的代码:
setup() {
const store = useStore();
//store.dispatch(Actions.LOAD_COMMUNITY);
onMounted(() => {
store.dispatch(Actions.LOAD_COMMUNITY);
setCurrentPageTitle(this.community.communityName);
});
return {
community: computed(() => store.getters.currentCommunity),
};
},
当我这样做时,出现“无法找到名称 'community'”错误,我无法使用 this.community.
修复它
如果你们能帮助我,我将不胜感激。
this
不应与组合 API 和 setup
一起使用。组件实例在 setup
中不可用,但在特殊情况下可以使用 getCurrentInstance()
访问。
应该是:
const community = computed(() => store.getters.currentCommunity),
onMounted(() => {
...
setCurrentPageTitle(unref(community).communityName);
});
return { community };
我从 vuex 中存储的数据库中获取数据。我在设置方法中使用 getter 检索它,但我想在呈现页面之前使用其中的一些数据,最好是在 onMounted 方法中。我不知道如何使用 Compositions API 来做到这一点。这是我的代码:
setup() {
const store = useStore();
//store.dispatch(Actions.LOAD_COMMUNITY);
onMounted(() => {
store.dispatch(Actions.LOAD_COMMUNITY);
setCurrentPageTitle(this.community.communityName);
});
return {
community: computed(() => store.getters.currentCommunity),
};
},
当我这样做时,出现“无法找到名称 'community'”错误,我无法使用 this.community.
修复它如果你们能帮助我,我将不胜感激。
this
不应与组合 API 和 setup
一起使用。组件实例在 setup
中不可用,但在特殊情况下可以使用 getCurrentInstance()
访问。
应该是:
const community = computed(() => store.getters.currentCommunity),
onMounted(() => {
...
setCurrentPageTitle(unref(community).communityName);
});
return { community };