1、通过使用prop来实现父子组件之间的传值,在父组件中,通过子组件标签中添加属性(props)来向子组件传递值,
父组件
<template>
<div>
<ChildComponent :childValue="parentValue" />
</div>
</template>
<script>
//引入子组件
import ChildComponent from './ChildComponent.vue';
export default {
components: {
ChildComponent
},
data() {
return {
parentValue: 'Hello' // 父组件的值
};
}
};
</script>
子组件
<template>
<div>
<p>子组件的值:{{ childValue }}</p>
</div>
</template>
<script>
export default {
props: ['childValue'] // 定义子组件接收的props
};
</script>