1、父组件向子组件传值

vue学习15:父子组件传值

vue学习15:父子组件传值

翻译如下:避免直接对道具进行修改,因为每当父组件重新呈现时,该值将被覆盖。相反,使用基于该道具的值的数据或计算属性。

也就是说,你不能直接在自组件里面去修改接收到的值,因为这样会导致其他组件的使用有问题,你应该在这个组件里面去改变属于它自己的数据或者属性。怎么写呢?代码如下:

[html] view plain copy
  1. props: ['count'],  
  2.    data () {  
  3.    return {  
  4.       number: this.count  
  5.         //把count的值给自己的number  
  6. }  
  7. },  
  8. template: `<div @click="handleCountClick">{{number}}</div>`,  
  9. methods: {  
  10.     handleCountClick() {  
  11.         this.number ++;  
  12.     }   
  13. }  

2、自组件向父组件传值

vue学习15:父子组件传值

this.$emit('change', 1) 1是携带的参数
handleChange(aaa) {//aaa接受携带的参数
this.total += aaa;
}

相关文章:

  • 2021-08-02
  • 2021-12-16
  • 2021-08-02
猜你喜欢
  • 2021-11-22
  • 2022-12-23
  • 2021-07-20
  • 2022-01-10
  • 2021-08-19
  • 2021-09-25
相关资源
相似解决方案