vue prop using variable
<template>
<div>
<h1>{{ message }}</h1>
<ChildComponent :passedProp="message" />
</div>
</template>
<script>
import ChildComponent from './ChildComponent.vue';
export default {
components: {
ChildComponent,
},
data() {
return {
message: 'Hello, world!',
};
},
};
</script>
Explanation:
- The
<template>
section defines the structure of the component. - Inside the
<div>
, there's an<h1>
tag that displays the value of themessage
data property using{{ message }}
. <ChildComponent :passedProp="message" />
passes themessage
data property as a prop namedpassedProp
to theChildComponent
.- The
<script>
section imports theChildComponent
from its file. - The Vue component definition includes
ChildComponent
in thecomponents
object. data()
is a function returning an object with a propertymessage
initialized to'Hello, world!'
.