父子动态传值
父:
首先引入子组件
import child from './child'
其次注册子组件:
components: {
child
}
在template里面引入子组件
<template>
<child :message = "opinion"></child>
</template>
子组件:
props: ['message']
或者
props:{
message: String
}
然后在created方法里面输出就能看到效果
created() {
console.log(this.message)
}
注意:父和子组件两者的“message”对应即可传无限参数
子组件调用父组件方法
父组件
<template>
<div>
<child @save="save"></child>
</div>
</template>
子组件
<template>
<div>
<button @click="a()"></button>
</div>
</template>
<script>
export default {
methods: {
a() {
this.$emit('save')
}
},
};
</script>