《小程序》父页面如果调用子页面的方法

第一步:首先先建立两个文件。 保证组件正常运行。子组件正常引入

第二步:在子组件中js中定义一个方法

methods: {
  childrenFn(){
    console.log('我是子组件的方法')
  }
}

 

第三步:在父组件WXML子组件定义一个ID

<v-children id = 'children'> </v-children>

第四步: 父页面js中调用子组件方法

onLoad: function (options) {
    let myView = this.selectComponent('#children')   // 打印出来的就是children 组件的实例了,
    myView.childrenFn()    //这里就可以输出子组件里面console.log 里的文字了
},

  

猜你喜欢

转载自www.cnblogs.com/yetiezhu/p/12632808.html
今日推荐