1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > Vue父组件调用子组件的方法(ref)

Vue父组件调用子组件的方法(ref)

时间:2021-04-22 18:05:28

相关推荐

Vue父组件调用子组件的方法(ref)

vue中如果父组件想调用子组件的方法,可以在子组件中加上ref,

然后通过this.$refs.ref.method调用,例如:

父组件:

<template>

<div @click="fatherMethod">

<child ref="child"></child>

</div>

</template>

<script>

import child from '~/components/dam/child.vue';

export default {

components: {

child

},

methods: {

fatherMethod() {this.$refs.child.childMethods();

}

}

};

</script>

子组件:

<template>

<div>{{name}}</div>

</template>

<script>

export default {

data() {

return {

name: '测试'

};

},

methods: {

childMethods() {

console.log(this.name);

}

}

};

</script>

在父组件中, this.$refs.child 返回的是一个vue实例,可以直接调用这个实例的方法

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