关于javascript:支付宝小程序父组件调用子组件方法

官网文档形容父组件不能调用子组件的办法 其实是能够的记录下以便日后应用

//子组件.axml
<text>我只是一个子组件</text>



//子组件.js
methods:{
    childMethod(){
        console.log("调用到了子组件的办法")
    }
}

//父组件.json

{
    child-compoent: '../componets/子组件门路'
}


//父组件.axml
<child-compoent ref="childMethods" />
//用ref来指向子组件


//父组件.js
{
    childMethods(ref){//注册子组件实例来调用子组件办法
        this.childCompoentMethod = ref
    },
    callChildCompoentMethods(){
        this.childCompoentMethod.childMethod()
    }
}

这样就能够调用到子组件的办法啦

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理