VueJS 3 / Vuex - 如何显示嵌套 json 结果中的数据

VueJS 3 / Vuex - How to display data from a nested json result

亲爱的,

我已经为此苦苦挣扎了几个小时,甚至在遵循基础教程之后也是如此。

我试图在我的 Vue 3 项目中显示 Json 中的一些数据,当数据在数组中时,使用循环我得到我需要的一切,但如果它不在数组,出现错误或数据粘在一起。

我创建了一个这样的 vuex 商店 :

const region = {
    state: {
        region: {}
    },
    mutations: {
        SET_REGION(state, region) {
            state.region = region
        }
    },
    actions: {
        getAllRegions({ commit }) {
            axios.get("/api/regions/get")
                .then(response => {
                    commit('SET_REGION', response.data)
                })
        }
    },
    getters: {
        getAllRegions (state) {
            return state.region
        },
        getDelegue (state) {
          return state.region.delegue
        }
    }
};

当我在我的页面中调用这个数据时,我得到了这个结果:

[
 {
  id: 3,
  name: "Occitanie",
  agents: [ ],
  delegue: null
 },
 {
  id: 2,
  name: "Ile de France",
  agents: [ ],
  delegue: null
 },
 {
  id: 4,
  name: "Hauts de France",
  agents: [ ],
  delegue: null
 },
 {
  id: 1,
  name: "Grand Est",
  agents: [
   {
    lastname: "DOE",
    firstname: "John",
    phone: "+331234567890",
    user: {
     email: "b@b.b"
    }
   }
  ],
  delegue: {
   lastname: "DURSLEY",
   firstname: "Jake",
   phone: "+3309987654321",
   user: {
    email: "a@a.a"
   }
  }
 }
]

结果对我来说似乎不错。现在在我的 Vue 中,我想显示数据,这就是我在“DELEGUE”数据方面遇到麻烦的地方。

<div v-for="region in myFunctionToRetrieveJsonData">
    <p>Name: {{ region.name }}</p> // WORKING

    <p v-for="agent in region.agents">
        {{ agent.lastname + ' ' + agent.firstname }}<br> // WORKING
        {{ agent.phone }}<br> // WORKING
        <span v-for="email in agent.user">{{ email }}</span> // WORKING

        // THIS WAY
        Delegue: {{ agent.delegue.lastname + ' ' + agent.delegue.firstname }} // NOT WORKING

        // ALSO THIS WAY
        Delegue: {{ agent.delegue[0].lastname + ' ' + agent.delegue[0].firstname }} // NOT WORKING

        // THE OTHER WAY
        <p v-for="delegue in region.delegue">
            Delegue: {{ delegue }} // DISPLAY: DURSLEYJake+3309987654321{"email":"a@a.a"} NOT GOOD
        </p>
    </p>
</div>

我收到这个错误: Uncaught (in promise) TypeError: Cannot read properties of null (reading 'lastname')

所以我知道对于 USERS 我可以做一个 v-for 循环,但是在 DELEGUE 的情况下,我不能马上访问,或者如果我做一个循环,我没有得到所有的数据分开了,我无法使用它。

你们有什么想法吗?

非常感谢您的帮助。

洛根

如果我理解正确,请看下面的片段:

new Vue({
  el: '#demo',
  data() {
    return {
      items: [
        {id: 3, name: "Occitanie", agents: [ ], delegue: null},
        {id: 2, name: "Ile de France", agents: [ ], delegue: null},
        {id: 4, name: "Hauts de France", agents: [ ], delegue: null},
        {id: 1, name: "Grand Est", agents: [{lastname: "DOE", firstname: "John", phone: "+331234567890", user: {email: "b@b.b"}}], delegue: {lastname: "DURSLEY", firstname: "Jake", phone: "+3309987654321", user: {email: "a@a.a"}}}
      ]
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="demo">
  <div v-for="region in items">
    <p>Name: {{ region.name }}</p>
    <p v-for="agent in region.agents">
       {{ agent.lastname + ' ' + agent.firstname }}<br> 
       {{ agent.phone }}<br> 
       <span v-for="email in agent.user">{{ email }}</span>
       Delegue: {{region.delegue?.lastname + ' ' + region.delegue?.firstname }}
    </p>
    
  </div>
</div>