【发布时间】:2020-04-08 12:06:27
【问题描述】:
我正在使用 Vuex 来存储一些数据。我正在尝试获取存储在 state 中的值,但我不知道如何从组件中检索它。
我想从如下所示的组件中加载 data 函数中的值。我可以使用this.$store.dispatch('currentUser/loginUser',this.user)成功保存一些数据,但我不知道如何获取。
这是 Vuex 模块的结构:
我要获取的数据在 loginSettings.js 文件中。我需要检索 closed 布尔值。这是文件里面的内容:
const state = {
loginModal:{
closed: false, // This is the value I need
}
};
const getters = {};
const actions = {};
const mutations = {};
export default {
namespaced: true,
state,
getters,
actions,
mutations
}
这是我组件中的脚本标签。
<script>
export default {
data: () => ({
dialog: this.$store.state.loginModal.closed, // This is throwing an error. The error is under this code block
user:{
email: "",
password: ""
}
}),
methods:{
login(){
this.$store.dispatch('currentUser/loginUser',this.user)
}
}
}
</script>
错误:Error in data(): "ReferenceError: $store is not defined"
使用 Vuex 使组件的属性反应的正确方法是什么?
【问题讨论】: