vue__组件自定义事件绑定、解绑、触发自定义事件、父子组件之间通信

我就是我 2024-04-05 07:10 248阅读 0赞

目录

总结:

组件的自定义事件

1.一种组件间通信的方式,

2.使用场景:

3.绑定自定义事件:

4.触发自定义事件:this.$emit(‘atguigu’,数据)

5.解绑自定义事件 this.$off(‘atguigu’)

6.组件上也可以绑定原生DOM事件,需要使用native修饰符

7.注意:通过this.$refs.xxx.$on(‘atguigu,回调)

一、组件自定义事件绑定

方式一

方式二

结果: 方式一和方式二的最终结果是一个样子的

二、组件自定义事件解绑


#

总结:

组件的自定义事件

1.一种组件间通信的方式,

适用于:子组件===>父组件

2.使用场景:

A是父组件,B是子组件B想给A传数据,那么就要在A中给B绑定自定义事件(事件的回调在A中)。

3.绑定自定义事件:

①第一种方式,

在父组件中:

②第二种方式,在父组件中:


…..
mounted(){
this.$refs.xxx.$on(‘atguigu’,this.test)}

fc63ede3257445248f18beb6ede4b5f2.png

ef0523f9704b4e82843bd4f2e91b6ca9.png

③若想让自定义事件只能触发一次,

可以使用once修饰符,或$once方法。

  1. this.$refs.student.$once('atguigu',this.getStudentName)

4.触发自定义事件:this.$emit(‘atguigu’,数据)

aa8909a927b04087b491a61ba71df499.png

5.解绑自定义事件 this.$off(‘atguigu’)

a806dceba9a842b7ad5246831df3b2c4.png

6.组件上也可以绑定原生DOM事件,需要使用native修饰符

beba999a9d034e3ea188adc5cc41259d.png

7.注意:通过this.$refs.xxx.$on(‘atguigu,回调)

绑定自定义事件时,回调要么配置在methods中,要么用箭头函数,否则this指向会出问题!

7736b0688c32443d8215474988c0f761.png

一、组件自定义事件绑定

方式一

app.vue

  1. <template>
  2. <div class="app">
  3. <h1>{
  4. {msg}}</h1>
  5. <!-- 通过父组件给子组件绑定一个自定义事件实现:子给父传递数据(第一种写法:使用@或v-on) -->
  6. <!-- v-on在student标签上,所以给这个组件的vc身上绑定一个事件atguigu,如果触发了这个事件,demo函数就会调用 -->
  7. <Student v-on:atguigu="getStudentName"></Student>
  8. </div>
  9. </template>
  10. <script>
  11. // 引入组件
  12. import School from './components/School'
  13. import Student from './components/Student'
  14. export default {
  15. name:'App',
  16. data(){
  17. return{
  18. msg:'你好啊!'
  19. }
  20. },
  21. // 注册组件
  22. components:{
  23. School,
  24. Student,
  25. },
  26. methods:{
  27. // 不论传过来多少个参数,第一个参数赋值给name,剩下的参数在params数组中
  28. getStudentName(name,...params){
  29. console.log('App收到了学生名',name)
  30. }
  31. },
  32. </script>
  33. <style scoped>
  34. .app{
  35. background-color:gray ;
  36. padding: 5px;
  37. }
  38. </style>

student.vue

  1. <template>
  2. <!-- <template>标签不参与编译,在页面展现的是下面的一段结构 -->
  3. <!-- 组件结构 -->
  4. <div class="student">
  5. <h2 > 学生名称:{
  6. {name}}</h2>
  7. <h2> 学生年龄:{
  8. {age}}</h2>
  9. <h2> 学生性别:{
  10. {sex}}</h2>
  11. <button @click="sendStudentName">把学生的名字给App</button>
  12. </div>
  13. </template>
  14. <script>
  15. export default {
  16. name:'Student',
  17. data(){
  18. return{
  19. name:'张三',
  20. sex:'男',
  21. age:18
  22. }
  23. },
  24. methods:{
  25. // 触发Student组件身上的atguigu事件
  26. sendStudentName(){
  27. // 这里的this指向vc
  28. // emit 爆发的含义
  29. // 注意!!这个地方要写事件名和传入的参数
  30. this.$emit('atguigu',this.name)
  31. }
  32. }
  33. }
  34. </script>
  35. <style scoped>
  36. .student{
  37. background-color: red;
  38. padding:5px;
  39. margin-top:30px;
  40. }
  41. </style>

方式二

  1. <template>
  2. <div class="app">
  3. <h1>{
  4. {msg}}</h1>
  5. <!-- 通过父组件给子组件绑定一个自定义事件实现:子给父传递数据(第二种写法:使用ref) -->
  6. <Student ref="student"/>
  7. </div>
  8. </template>
  9. <script>
  10. // 引入组件
  11. import School from './components/School'
  12. import Student from './components/Student'
  13. export default {
  14. name:'App',
  15. data(){
  16. return{
  17. msg:'你好啊!'
  18. }
  19. },
  20. // 注册组件
  21. components:{
  22. School,
  23. Student,
  24. },
  25. methods:{
  26. // 不论传过来多少个参数,第一个参数赋值给name,剩下的参数在params数组中
  27. getStudentName(name,...params){
  28. console.log('App收到了学生名',name)
  29. }
  30. },
  31. // 挂载完毕
  32. mounted(){
  33. // this.$refs.student获取真实的DOM元素
  34. // $on可以理解为当....时候 $on('atguigu')当atguigu事件被触发的时候
  35. this.$refs.student.$on('atguigu',this.getStudentName)
  36. // 下面这个只能触发一次
  37. // this.$refs.student.$once('atguigu',this.getStudentName)
  38. }
  39. }
  40. </script>
  41. <style scoped>
  42. .app{
  43. background-color:gray ;
  44. padding: 5px;
  45. }
  46. </style>
  47. <template>
  48. <!-- <template>标签不参与编译,在页面展现的是下面的一段结构 -->
  49. <!-- 组件结构 -->
  50. <div class="student">
  51. <h2 > 学生名称:{
  52. {name}}</h2>
  53. <h2> 学生年龄:{
  54. {age}}</h2>
  55. <h2> 学生性别:{
  56. {sex}}</h2>
  57. <button @click="sendStudentName">把学生的名字给App</button>
  58. </div>
  59. </template>
  60. <script>
  61. export default {
  62. name:'Student',
  63. data(){
  64. return{
  65. name:'张三',
  66. sex:'男',
  67. age:18
  68. }
  69. },
  70. methods:{
  71. // 触发Student组件身上的atguigu事件
  72. sendStudentName(){
  73. // 这里的this指向vc
  74. // emit 爆发的含义
  75. // 注意!!这个地方要写事件名和传入的参数
  76. this.$emit('atguigu',this.name)
  77. }
  78. }
  79. }
  80. </script>
  81. <style scoped>
  82. .student{
  83. background-color: red;
  84. padding:5px;
  85. margin-top:30px;
  86. }
  87. </style>

结果: 方式一和方式二的最终结果是一个样子的

c12c53fd39764f13b3eb7762527693e1.png

二、组件自定义事件解绑

  1. <template>
  2. <!-- <template>标签不参与编译,在页面展现的是下面的一段结构 -->
  3. <!-- 组件结构 -->
  4. <div class="student">
  5. <h2 > 学生名称:{
  6. {name}}</h2>
  7. <h2> 学生年龄:{
  8. {age}}</h2>
  9. <h2> 学生性别:{
  10. {sex}}</h2>
  11. <button @click="sendStudentName">把学生的名字给App</button>
  12. <button @click="unbind">解绑atguigu事件</button>
  13. </div>
  14. </template>
  15. <script>
  16. export default {
  17. name:'Student',
  18. data(){
  19. return{
  20. name:'张三',
  21. sex:'男',
  22. age:18
  23. }
  24. },
  25. methods:{
  26. // 触发Student组件身上的atguigu事件
  27. sendStudentName(){
  28. // 这里的this指向vc
  29. // emit 爆发的含义
  30. // 注意!!这个地方要写事件名和传入的参数
  31. this.$emit('atguigu',this.name)
  32. },
  33. unbind(){
  34. // 这个语句只能解绑一个自定义事件
  35. this.$off('atguigu')
  36. // this.$off(['atguigu','demo']) 解绑多个事件,有几个事件,就往里面写几个事件
  37. // this.$off() 解绑所有的自定义事件
  38. }
  39. }
  40. }
  41. </script>
  42. <style scoped>
  43. .student{
  44. background-color: red;
  45. padding:5px;
  46. margin-top:30px;
  47. }
  48. </style>

651bb1516b4d44a9a571d64c5f9faff3.png

当我们解绑后,无论点多少次“把学生的名字给App”都不会管用

发表评论

表情:
评论列表 (有 0 条评论,248人围观)

还没有评论,来说两句吧...

相关阅读