父组件向子组件传值
父组件Home
<template>
<div class="home">
<Head :title='msg'></Head>
</div>
</template><script>
import Head from '@/components/Head.vue'export default {
name: 'Home',
components: {
Head
},
data(){
return {
msg:"父组件的值"
}
}
}
</script>
子组件Head
<template>
{{title}}
</template><script>
export default{
name:'Head',
props:['title']
}
</script><style>
</style>
子组件向父组件传值
子组件Head
<template>
head组件<br />
{{title}}<hr />
<button @click="send">向父组件传值</button>
</template><script>
export default{
name:'Head',
props:['title'],
methods:{
send(){
this.$emit("receive","子组件的值")
}
}
}
</script><style>
</style>
父组件Home
<template>
<div class="home">
<Head :title='msg'
@receive="printMsg"
></Head>
</div>
</template><script>
import Head from '@/components/Head.vue'export default {
name: 'Home',
components: {
Head
},
data(){
return {
msg:"父组件的值"
}
},
methods:{
printMsg(msg){
console.log(msg)
}
}
}
</script>