Javascript Vue mapState非反应性
我尝试使用mapState函数从存储中获取状态,但无法使用生成的代码将值返回到模板代码中Javascript Vue mapState非反应性,javascript,vue.js,vuejs2,vuex,Javascript,Vue.js,Vuejs2,Vuex,我尝试使用mapState函数从存储中获取状态,但无法使用生成的代码将值返回到模板代码中 <template> // Some code <template v-if="!isLoggedIn"> // Some code </template> <template v-else>
<template>
// Some code
<template v-if="!isLoggedIn">
// Some code
</template>
<template v-else>
// Some code
{{ currentUser.name }}
</template>
// Some code
</template>
<script>
import { mapState } from "vuex";
export default {
// Some code
computed: {
...mapState({auth : ['currentUser', 'isLoggedIn','customers']})
}
}
</script>
//一些代码
//一些代码
//一些代码
{{currentUser.name}
//一些代码
从“vuex”导入{mapState};
导出默认值{
//一些代码
计算:{
…映射状态({auth:['currentUser','isLoggedIn','customers']})
}
}
相反,下面的代码可以工作
<script>
import { mapState } from "vuex";
export default {
// Some code
computed: {
currentUser() {
return this.$store.state.auth.currentUser
},
isLoggedIn() {
return this.$store.state.auth.isLoggedIn
},
}
}
</script>
从“vuex”导入{mapState};
导出默认值{
//一些代码
计算:{
当前用户(){
返回此。$store.state.auth.currentUser
},
伊斯洛格丁(){
返回此。$store.state.auth.isLoggedIn
},
}
}
警告信息
[Vue warn]:属性或方法“isLoggedIn”未在实例上定义,但在渲染期间被引用。通过初始化属性,确保此属性在数据选项中或对于基于类的组件是被动的。请参阅:
提前感谢如果您试图从名为
auth
的命名空间vuex模块访问值,请将模块名称作为第一个参数传递,将要映射的值数组作为第二个参数传递:
computed: {
...mapState('auth', ['currentUser', 'isLoggedIn','customers'])
}
您可以映射模块状态,然后使用this.auth.isLoggedin来访问非根属性的正确语法如下(使用箭头函数):
查看。我们需要了解Vuex存储和模块的基本结构。请原谅,谢谢你!如果它不起作用,这个语法对mapActions有效,但对mapActions无效mapState@Aldarund该语法适用于
mapState
。但是,决定因素是模块是否具有名称空间。它不适用于没有名称空间的模块。@aaronofleonard hm,对,刚刚检查过,它可以工作。但是我在docs->@Aldarund中没有看到这样的例子是的,docs似乎没有。我试图找到一个关于这个的链接,把它放在我的答案里。
computed: {
...mapState({
currentUser: state => state.auth.currentUser,
isLoggedIn: state => state.auth.isLoggedIn,
customers: state => state.auth.customers
})}