$root
vue狀態(tài)管理使用vuex,如果項(xiàng)目不大,邏輯不多,name我們沒必要用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能夠訪問父組件的屬性和方法
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>$refs
總結(jié)
以上所述是小編給大家介紹的vue中的$root,$parent,希望對(duì)大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注