小程序中的父子组件之间的通信

1.父组件向子组件通信

父组件的写法:这里通过current="{ {current}}"来向子组件传值

<more-option bind:reVal="refresh"  current="{
     
     {current}}"/>

子组件中在properties中获取

 properties: {
    
    
    current: {
    
    
      type: Number,
      default: 0
    }
  },

然后直接可以在methods中的某个方法中获取就可以了

methods:{
    
    
    //这里是随便一个方法名字
   getNum(){
    
    
     console.log(this.data.current);    //这样就可以获取到父组件传过来的值了
     }
}

2.子组件向父组件通信

子组件的写法:

postVal(params) {
    
    
   this.triggerEvent('reVal', params)//这里的refresh是自己自定义的事件名字,父组件需要通过它来接收子组件传过来的值
 }

父组件的写法:

<more-option bind:reVal="refresh"  current="{
     
     {current}}"/>
//这里的reVal是子组件传递过来的自定义事件名称,refresh这个是父组件中自定义的事件名称

然后在父组件的methods中手动获取就好了

refresh(params) {
    
    
   console.log(params)
 },

猜你喜欢

转载自blog.csdn.net/weixin_45324044/article/details/108618563