穷则独善其身,达则兼善天下。没有梦想的人到达不了成功的彼岸,也就因此而看不到成功的辉煌。没有梦想的人生是失败的,因为他们根本看不到生命的意义。
先定义一个子组件,在组件中注册props
<template>
<div>
<div>{{message}}(子组件)</div>
</div>
</template>
<script>
export default {
props: {
message: String //定义传值的类型<br> }
}
</script>
<style>
</style>
在父组件中,引入子组件,并传入子组件内需要的值
<template>
<div>
<div>父组件</div>
<child :message="parentMsg"></child>
</div>
</template>
<script>
import child from './child' //引入child组件
export default {
data() {
return {
parentMsg: 'a message from parent' //在data中定义需要传入的值
}
},
components: {
child
}
}
</script>
<style>
</style>
这种方式只能由父向子传递,子组件不能更新父组件内的data
本文详解vue父子组件间传值(props)到此结束。如果你看到自己的影子,同时也应感受到阳光的存在。小编再次感谢大家对我们的支持!