小程序:父子組件方法的相互調用

一、父組件調用子組件方法

子組件方法:

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

父組件調用:

<test id="two"/>

 在自定義方法裏調用即可

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

 


二、子組件調用父組件方法

父組件方法

<test  bind:submit="submit" ></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')
        }
     }
})

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章