App.vue
InfoBox.vue
GrandChild.vue
main.js
<template>
<h2>Example $root Object</h2>
<p v-bind:style="{ backgroundColor: color }">The 'color' data property can be reached and manipulated directly from a child component, even far down the component tree structure, by the use of the $root object.</p>
<info-box />
</template>
<script>
export default {
data() {
return {
color: 'pink'
}
}
}
</script>
<template>
<div>
<h3>Child Component</h3>
<grand-child />
</div>
</template>
<style scoped>
div {
border: solid black 1px;
padding: 10px;
width: 250px;
}
</style>
<template>
<div>
<h4>Grand Child Component</h4>
<p>Click the button to toggle the color of the P tag in the root component.</p>
<button v-on:click="this.$root.color='lightgreen'">Change color in root</button>
</div>
</template>
<style scoped>
div {
border: solid black 1px;
padding: 10px;
width: 100%;
box-sizing: border-box;
}
</style>
import { createApp } from 'vue'
import App from './App.vue'
import InfoBox from './components/InfoBox.vue'
import GrandChild from './components/GrandChild.vue'
const app = createApp(App)
app.component('info-box', InfoBox)
app.component('grand-child', GrandChild)
app.mount('#app')