【发布时间】:2017-10-23 20:15:59
【问题描述】:
我有以下Navigation.vue 组件:
<template>
<div>
{{user.first_name}}
</div>
</template>
<script>
import { mapActions, mapGetters } from 'vuex'
export default {
name: 'hello',
methods: {
...mapActions(['myAccount'])
},
mounted: function () {
if (localStorage.getItem('access_token')) {
this.myAccount()
}
},
computed: {
...mapGetters(['user'])
}
}
</script>
此代码返回:
[Vue warn]: Error in render function: "TypeError: Cannot read property 'first_name' of null"
但奇怪的是用户名显示正确。我做错了什么?
【问题讨论】:
-
我假设这是因为,在您的 vuex 存储中,
user的状态被初始化为null。只需将user初始化为一个空对象{}。 -
添加您的评论作为答案,我会接受。
标签: javascript vue.js vuex