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

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

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

$root

vue狀態(tài)管理使用vuex,如果項(xiàng)目不大,邏輯不多,name我們沒(méi)必要用vuex給項(xiàng)目增加難度,只需要用$root設(shè)置vue實(shí)例的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能夠訪問(wèn)父組件的屬性和方法

parent.vue

<template>
  <div>
    <child>
    </child>
  </div>
</template>
<script> 
  import child from './child';
  export default {
    components:{
      child 
    },
    data(){
      return {
        text:"測(cè)試"
      }
    },
  }
</script>

child.vue

<template>
  <div>
    <child>
    </child>
  </div>
</template>
<script> 
  import child from './child';
  export default {
    created(){
    console.log(this.$parent.text)//測(cè)試(能夠獲取到父組件的屬性和方法)
   }
  }
</script>

看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)的支持。

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

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶(hù)體驗(yàn)、微信小程序、域名注冊(cè)動(dòng)態(tài)網(wǎng)站、云服務(wù)器、網(wǎng)站策劃

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

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