Vue父组件如何调用子组件函数?
- 内容介绍
- 文章标签
- 相关推荐
本文共计347个文字,预计阅读时间需要2分钟。
在父组件中调用子组件的方法:
1. 给子组件定义一个ref属性。 eg: ref=childItem
2.在子组件的methods中声明一个函数。
eg: useInPar: function (str) {console.log(str)}3.在父组件中声明一个函数,并调用子组件的ref属性来调用子组件的方法。
eg: function callChildMethod() {this.childItem.useInPar(Hello, child component!)}在父组件中调用子组件的方法:
1.给子组件定义一个ref属性。eg:ref="childItem"
2.在子组件的methods中声明一个函数。eg: useInPar:function (str) {console.log(str)}
2. 在父组件的中声明一个函数,并通过this.$refs.childItem.userInPar来使用子组件中声明的函数。
父组件:
<template> <child-item ref='child' /> <button @click='useChildFun'></button> </template> <script> ``` methods() { useChildFun:function(){ this.$refs.child.usedInPar('调用子组件中的方法'); } } </script>
子组件:
``` methods () { usedInPar(str){ console.log(str); } }
没有完整的代码,只有一个用法
总结
以上所述是小编给大家介绍的vue 父组件中调用子组件函数的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对自由互联网站的支持!
本文共计347个文字,预计阅读时间需要2分钟。
在父组件中调用子组件的方法:
1. 给子组件定义一个ref属性。 eg: ref=childItem
2.在子组件的methods中声明一个函数。
eg: useInPar: function (str) {console.log(str)}3.在父组件中声明一个函数,并调用子组件的ref属性来调用子组件的方法。
eg: function callChildMethod() {this.childItem.useInPar(Hello, child component!)}在父组件中调用子组件的方法:
1.给子组件定义一个ref属性。eg:ref="childItem"
2.在子组件的methods中声明一个函数。eg: useInPar:function (str) {console.log(str)}
2. 在父组件的中声明一个函数,并通过this.$refs.childItem.userInPar来使用子组件中声明的函数。
父组件:
<template> <child-item ref='child' /> <button @click='useChildFun'></button> </template> <script> ``` methods() { useChildFun:function(){ this.$refs.child.usedInPar('调用子组件中的方法'); } } </script>
子组件:
``` methods () { usedInPar(str){ console.log(str); } }
没有完整的代码,只有一个用法
总结
以上所述是小编给大家介绍的vue 父组件中调用子组件函数的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对自由互联网站的支持!

