1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > Vue父组件调用子组件的方法并传参的两种方式(用$refs.refName.functionName window.function)

Vue父组件调用子组件的方法并传参的两种方式(用$refs.refName.functionName window.function)

时间:2018-09-06 08:32:51

相关推荐

Vue父组件调用子组件的方法并传参的两种方式(用$refs.refName.functionName window.function)

如需了解儿子怎么控制老子的,传送门:https://s-z-q./article/details/14689

父组件father.vue

<template><ul><li> <son ref="son" /> </li><br><li> <button @click="$refs.son.func(true)"> 父组件调用子组件方法1:$refs.refName.function </button> </li><br><li> <button @click="windown_func(true)"> 父组件调用子组件方法2:window.function </button> </li></ul></template><script>import son from "./son";export default {components: {son,},methods: {windown_func(d) {window.func(d);},},};</script>

子组件son.vue

<template><div class="sg-son"><button @click="func()">子组件调用自己的方法</button></div></template><script>export default {created() {window.func = this.func; //声明全局方法(直男模式)},methods: {func(isFather) {alert(isFather ? "父组件调用子组件方法" : "子组件调用自己的方法");},},};</script>

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。