我的数据库中有嵌套对象这样的数据:
[{"id":"1-368","name":"\tSolan","days":1,"daynights":2,"hotel":{"hotel_data":[{"id":1,"title":"hotel 1"}],"checkin":"","checkout":"","roomtype":""},"hotel2":{"hotel_data":[{"id":1,"title":"hotel 1"}],"checkin":"","checkout":"","roomtype":""},"specinst":"","mealplan":""},{"id":"2-54","name":"Dharamsala","days":"3","daynights":4,"hotel":{"hotel_data":[{"id":3,"title":"hotel3"}],"checkin":"","checkout":"","roomtype":""},"hotel2":{"hotel_data":[{"id":2,"title":"hotel 2"}],"checkin":"","checkout":"","roomtype":""},"specinst":"","mealplan":""}]
现在,当我尝试获取数据并尝试通过突变将状态分配给状态时,它会像以下那样被不完全分配:
[{"id":"1-368","name":"\tSolan","days":1,"daynights":2,"hotel":{"hotel_data":"","checkin":"","checkout":"","roomtype":""},"hotel2":{"hotel_data":"","checkin":"","checkout":"","roomtype":""},"specinst":"","mealplan":"","date_from":"08 Jan 2020","date_to":"09 Jan 2020"},{"id":"2-54","name":"Dharamsala","days":"3","daynights":4,"hotel":{"hotel_data":"","checkin":"","checkout":"","roomtype":""},"hotel2":{"hotel_data":"","checkin":"","checkout":"","roomtype":""},"specinst":"","mealplan":"","date_from":"09 Jan 2020","date_to":"12 Jan 2020"}]
在上面的代码中,您会注意到hotel_data
是一个嵌套数组,但是在将其分配为vuex
状态后看不到。
代码:
const mutations = {
setItem(state, item) {
state.item.tour_location=JSON.parse(item.tour_location);
}}
答案 0 :(得分:0)
如果console.log(item.tour_location);
突变内的setItem
,您会看到什么?如果数据以正确的结构输入,我建议将您的状态设置为预期数据的框架。
例如:
预期数据:{id: 'abc', hotel: [{ prop1: '123', prop2: '234']}
Vuex状态:{id: '', hotel: [{ prop1: '', prop2: '' }]}
希望这会有所帮助。