vue 向父级组件发送消息

Contents

组件内用$emit定义事件名称,传递参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<template>
<p @click="run">textComponent 组件</p>
</template>
<script>
export default {
name: "test",
data () {
return {
content: "哈哈"
};
},
methods: {
run () {
this.$emit("textEmit", this.content);
}
}
};
</script>

父组件调用事件,获得组件传递的数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

<template>
<div>
<du-s @textEmit="text"></du-s>
</div>
</div>
</template>
<script>
export default {
methods: {
text (q) {
console.log(q);
}
}
};

结果输出为:哈哈