小程序:父子组件方法的相互调用

一、父组件调用子组件方法

子组件方法:

//    组件的方法列表
    methods: {
        test(){
          console.log('我是子组件方法')
        }
   },

父组件调用:

 在自定义方法里调用即可

this.selectComponent('#two').test();

 


二、子组件调用父组件方法

父组件方法


submit(event) {
    console.log(event);
},

子组件用this.triggerEvent('父组件自定义事件', '要传递的参数'),触发父组件传过来的自定义事件

 Component({
     properties: {
        totalMoney: {
          type: Number,
          value: 0,   //默认
        }
     },
     methods:{
         //子组件事件
         btnclick(){
            console.log('aaaaaaaaaaaaaaaaaaa');
            this.triggerEvent("submit", 'Hello lily')
        }
     }
})

 

你可能感兴趣的:(小程序开发,小程序)