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

8次阅读

共计 383 个字符,预计需要花费 1 分钟才能阅读完成。

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

// 子组件.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()
    }
}

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

正文完
 0