vue中$root與$parent的區(qū)別是什么

vue中$root與$parent的區(qū)別是什么?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

創(chuàng)新互聯(lián)建站專業(yè)為企業(yè)提供寧陽網(wǎng)站建設(shè)、寧陽做網(wǎng)站、寧陽網(wǎng)站設(shè)計、寧陽網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計與制作、寧陽企業(yè)網(wǎng)站模板建站服務(wù),10余年寧陽做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡(luò)服務(wù)。

$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)的支持。

新聞名稱:vue中$root與$parent的區(qū)別是什么
URL分享:http://muchs.cn/article24/isgice.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗、微信小程序域名注冊、動態(tài)網(wǎng)站云服務(wù)器網(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)

成都網(wǎng)站建設(shè)公司