鍍金池/ 問(wèn)答/網(wǎng)絡(luò)安全  HTML/ 關(guān)于vue的this.$root的問(wèn)題

關(guān)于vue的this.$root的問(wèn)題

$res_date = { user_name: "Admin" };

this.$root.username = $res_date.user_name;
console.log(this.$root.usernsme);

報(bào)錯(cuò): Cannot set property 'username' of undefined

提示了username 不存在,應(yīng)該怎么寫
對(duì)vue不太熟.. 望指教~

回答
編輯回答
安淺陌

1.錯(cuò)誤的意思是你不應(yīng)該給一個(gè)不存在的屬性賦值,可以是vue做了處理
2.為什么要把data掛在root上,如果需要在多個(gè)組件之間傳遞數(shù)據(jù),可以用vuex

2017年11月12日 19:07
編輯回答
離夢(mèng)

this是啥,是vue實(shí)例嗎?

2017年3月2日 20:05
編輯回答
遲月

很多時(shí)候這個(gè)時(shí)候讀不出來(lái)屬性都是因?yàn)槟阍谝粋€(gè)錯(cuò)誤的生命周期階段讀了一個(gè)還不存在的屬性

2018年4月19日 15:40