vue中$root與$parent的區(qū)別是什么?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
$root
vue狀態(tài)管理使用vuex,如果項目不大,邏輯不多,name我們沒必要用vuex給項目增加難度,只需要用$root設(shè)置vue實例的data就行了,如下
main.js
new Vue({ data(){ return{ loading:true } }, router, store, render: h => h(App) }).$mount('#app')
a.vue
created(){ console.log(this.$root.loading) //獲取loading的值 }
b.vue
created(){ this.$root.loading = false; //設(shè)置loading的屬性 }
$parent
parent能夠訪問父組件的屬性和方法
parent.vue
<template> <div> <child> </child> </div> </template> <script> import child from './child'; export default { components:{ child }, data(){ return { text:"測試" } }, } </script>
child.vue
<template> <div> <child> </child> </div> </template> <script> import child from './child'; export default { created(){ console.log(this.$parent.text)//測試(能夠獲取到父組件的屬性和方法) } } </script>
看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)的支持。
文章標(biāo)題:vue中$root與$parent的區(qū)別是什么-創(chuàng)新互聯(lián)
文章分享:http://www.rwnh.cn/article22/ceijcc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開發(fā)、網(wǎng)站設(shè)計公司、品牌網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計公司、網(wǎng)站改版、定制網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容