0

我将数据存储在 Vuex 商店中。这里的样子:

在此处输入图像描述

基本上,我想在视图中显示 first_name 。所以我返回了这样的数据:

data () {
        return {
            first_name: this.$store.state.user.attributes.first_name,
        }
    },

Ans 很容易想在 div 中显示它:

<div>{{first_name}}</div>

但我得到vue.esm.js:1906 TypeError: Cannot read properties of undefined (reading 'first_name')

4

1 回答 1

0

为此,您需要一个计算属性。

因此,不要将变量first_name放在您的 中data,而是将其设置为计算属性,如下所示:

computed: { first_name() { return this.$store.state.user.attributes.first_name } }

于 2022-02-11T13:01:13.780 回答