gpt4 book ai didi

javascript - Vue mapState 非 react 性

转载 作者:搜寻专家 更新时间:2023-10-30 22:50:53 26 4
gpt4 key购买 nike

我尝试使用 mapState 函数从商店获取状态,但我无法使用将值返回到我的模板代码中的生成代码...

<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>

代替下面的代码工作

<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>

警告信息

[Vue warn]: Property or method "isLoggedIn" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property. See: https://v2.vuejs.org/v2/guide/reactivity.html#Declaring-Reactive-Properties.

提前致谢

最佳答案

访问非根属性的正确语法如下(使用箭头函数):

computed: {
...mapState({
currentUser: state => state.auth.currentUser,
isLoggedIn: state => state.auth.isLoggedIn,
customers: state => state.auth.customers
})}

检查 documentation .

关于javascript - Vue mapState 非 react 性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51391256/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com