标签:传值 事件 通信 eve default 位置 传递 ops template
<template>
<div>
<Child :title="title"> </Child>//子组件
</div>
</template>
props:{
title:{
type:String,
default:'我是标题'
}
}
<div>{{title}}</div>
<template>
<div>
<Child ref="childId"> </Child>//子组件
//注:ref相当于dom里的id,在js里边调用方式是:this.$refs.childId
</div>
</template>
this.$refs.childId.数据或方法
总结:
1.定义一个事件和方法toParent,来向父组件传值
2.在这个toParent方法中,使用$emit发送数据(也叫做广播数据)
this.$emit('任意发送数据的名称',要发射的数据)
1.在契合点的位置,定义自定义事件和方法 (和子组件的方法名保持一致)
//子组件Child.vue
<template>
<div>
<button @click="toParent">降价</button></p>ildId
</div>
</template>
子组件的methods:
toParent(count){
this.$emit('toParent',this.count)
}
// toParent是传递给父组件的事件,父组件触发、并相应这个方法
// this.data 传递给父组件的参数,,在父组件中,可以对和这个参数进行相应操作
<template>
<div>
<Child @toParent="toParent"></Child>
</div>
</template>
父组件的methods:
toParent(count) {
this.price = count + 10;
}
import Vue from 'vue'
export default new Vue()
import eventBus from '../eventBus.js'
<button @click="emitSon2">发出数据</button>
//方法
methods:{
emitSon2(){
eventBus.$emit('toSon2',this.name)
}
}
mounted:{
eventBus.$on("toSon2", (data) => {
console.log(data)
});
}
标签:传值 事件 通信 eve default 位置 传递 ops template
原文地址:https://www.cnblogs.com/maizilili/p/12402125.html