Vue子组件向父组件传递数据
知识点
组件的自定义事件
一种组件间通信的方式,适用于:子组件 ===> 父组件
使用场景:A是父组件,B是子组件,B想给A传数据,那么就要在A中给B绑定自定义事件(事件的回调在A中)。
绑定自定义事件:
第一种方式,在父组件中:
<Demo @atguigu="test"/>
或<Demo v-on:atguigu="test"/>
第二种方式,在父组件中:
<Demo ref="demo"/> ...... mounted(){ this.$refs.xxx.$on('atguigu',this.test) }
若想让自定义事件只能触发一次,可以使用
once
修饰符,或$once
方法。
触发自定义事件:
this.$emit('atguigu',数据)
解绑自定义事件
this.$off('atguigu')
组件上也可以绑定原生DOM事件,需要使用
native
修饰符。注意:通过
this.$refs.xxx.$on('atguigu',回调)
绑定自定义事件时,回调要么配置在methods中,要么用箭头函数,否则this指向会出问题!
方式一: 通过父组件给子组件传递函数类型的props实现
App.vue 父组件
<template>
<div>
<h1 class="title">{{ title + ' ' }}学校名称: {{ schoolName }} </h1>
<!-- 子组件给父组件传递数据,通过父组件给子组件传递函数类型的props实现-->
<School :getSchoolName="getSchoolName"/>
</div>
</template>
<script>
import School from '@/components/School.vue'
export default {
name: 'App',
components: {School},
data() {
return {
title: '你好',
schoolName: ''
}
},
methods: {
getSchoolName(name) {
this.schoolName = name
}
}
}
</script>
School .vue 子组件
<template>
<div class="demo">
<h2>学校名称:{{ name }}</h2>
<h2>学校地址:{{ address }}</h2>
<!-- 点击,子组件向父组件传值 -->
<button @click="sendSchoolName">给APP父组件传递学校名称</button>
</div>
</template>
<script>
export default {
name: 'School',
data() {
return {
name: '尚硅谷atguigu',
address: '北京'
}
},
props: ['getSchoolName'],
methods: {
sendSchoolName() {
this.getSchoolName(this.name)
}
}
}
</script>
<style scoped>
.demo {
background-color: skyblue;
margin-bottom: 30px;
padding: 10px;
}
</style>
方式二:通过父组件给子组件绑定一个自定义事件实现
App.vue 父组件
<template>
<div>
<h1 class="title">{{ title + ' ' } 学生姓名:{{ studentName }}</h1>
<!-- 通过父组件给子组件绑定一个自定义事件实现,(第一种写法,使用@或v-on)-->
<!-- v-on在谁身上就是再给谁绑定事件, 事件名叫atguigu,getStudentName函数就会被调用 -->
<!--<Student v-on:atguigu="getStudentName"/>-->
<!-- 只触发一次 -->
<!-- <Student @atguigu.once="getStudentName"/>-->
<!-- 通过父组件给子组件绑定一个自定义事件实现,(第二种写法,使用ref)-->
<Student ref="student"/>
</div>
</template>
<script>
import Student from '@/components/Student.vue'
export default {
name: 'App',
components: { Student},
data() {
return {
title: '你好',
studentName: ''
}
},
mounted() {
// this.$refs.student.$on('atguigu', this.getStudentName) // 绑定自定义事件
this.$refs.student.$once('atguigu', this.getStudentName) // 绑定自定义事件(只触发一次)
},
methods: {
getStudentName(name) {
this.studentName = name
},
// 传递多个参数时
//getStudentNames(name, ...params) {
// this.studentName = name
//}
,
add(){
this.number++
},
unbind(){
// 解绑一个
this.$off('atguigu')
// 解绑多个
this.$off(['atguigu', 'demo'])
// 解绑所有的自定义事件
this.$off()
},
death(){
this.$destroy() // 销毁当前student组件的实例,销毁后所有实例的自定义事件全部不奏效。
}
}
}
</script>
<style scoped>
</style>
School .vue 子组件
<template>
<div class="demo">
<h2>学生姓名:{{ name }}</h2>
<h2>学生性别:{{ sex }}</h2>
<h2>当前求和:{{ number }}</h2>
<button @click="sendStudentName">发送学生姓名</button>
<button @click="add">点我number++</button>
<button @click="unbind">解绑aiguigu事件</button>
<button @click="death">销毁当前student组件的实例Vc</button>
</div>
</template>
<script>
export default {
name: 'Student',
data() {
return {
name: '张三',
sex: '男',
number: 0
}
},
methods: {
sendStudentName() {
// 触发事件,传递参数
this.$emit('atguigu', this.name)
//this.$emit('atguigu', this.name, 44, 23, 21)
}
}
}
</script>
<style lang="less" scoped>
.demo {
background-color: pink;
padding: 10px;
}
</style>