有时候我们经常分不清什么是父组件,什么又是子组件。现在来简单说下:我们将某段代码封装成一个组件,而这个组件又在另一个组件中引入,而引入该封装的组件的文件叫做父组件,被引入的组件叫做子组件。以上是我个人的理解含义,那么现在就直接上代码吧!
子组件:
<template>
<div>
{
{msg}}
</div>
<p>{
{hello}}</p>
<p>{
{hellotests}}</p>
<button @click="btns">按钮</button>
</template>
<script>
export default{
data(){
return{
// 子组件中声明的变量
msg:'',
hello:'',
hellotests:''
}
},
created(){
},
watch:{
// 监听从父组件传过来的数据是否发生改变,然后赋值给变量
CCCount() {
this.msg = this.msgTest
this.hello = this.helloTest
}
},
methods:{
// 点击按钮,子组件操做父组件中的函数
btns(){
this.$emit('childsClick', '子组件触发父组件中的函数')
},
// 被父组件调用的子组件函数
msgs1(){
this.hellotests = '哈哈,我是父组件调用子组