App.vue
InfoBox.vue
main.js
<template>
<h2>Example $parent Object</h2>
<p v-bind:style="{ backgroundColor: color }">The method that changes color can be reached directly from the child component by the use of the $parent object.</p>
<div>
<info-box />
</div>
</template>
<script>
export default {
data() {
return {
color: 'lightgreen'
}
},
methods: {
toggleColor() {
if(this.color === 'lightgreen'){
this.color = 'pink'
}
else {
this.color = 'lightgreen'
}
}
}
}
</script>
<template>
<div>
<h3>Toggle Color</h3>
<p>Click the button to toggle the color in the P tag of the parent component.</p>
<button v-on:click="this.$parent.toggleColor">Toggle</button>
<p>The 'toggleColor' method is also in the parent component.</p>
</div>
</template>
<style scoped>
div {
border: solid black 1px;
padding: 10px;
width: 250px;
}
</style>
import { createApp } from 'vue'
import App from './App.vue'
import InfoBox from './components/InfoBox.vue'
const app = createApp(App)
app.component('info-box', InfoBox)
app.mount('#app')