微信小程序頁面調用自定義組件內的事件

微信小程序頁面調用自定義組件內的事件

page

page.json

{
    "usingComponents": {
        "my-component": "../components/component/component",
    }
}

  

page.wxml

<my-component id="myComponent"></my-component>

  

page.js

Page({
  //...
  onLoad: function () {
    let myComponent = this.selectComponent('#myComponent'); // 頁面獲取自定義組件實例
    myComponent.componentInnerFunction(); // 經過實例調用組件事件
  }
  //...
})

  

component

component.js

Component({
    //...
    methods: {
        componentInnerFunction: function() {
            console.log('i am inner function');
        }
    }
    //...
})
相關文章
相關標籤/搜索