Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[vue] 如何在子组件中访问父组件的实例? #477

Open
haizhilin2013 opened this issue Jun 22, 2019 · 7 comments
Open

[vue] 如何在子组件中访问父组件的实例? #477

haizhilin2013 opened this issue Jun 22, 2019 · 7 comments
Labels
vue vue

Comments

@haizhilin2013
Copy link
Collaborator

[vue] 如何在子组件中访问父组件的实例?

@haizhilin2013 haizhilin2013 added the vue vue label Jun 22, 2019
@yin-pathfinder
Copy link

this.$parent

@lovelmh13
Copy link

this.$parent拿到父组件实例
this.$children拿到子组件实例(数组)

@qq326943819
Copy link

this.$parent

@HeMin0919
Copy link

vue中如果父组件想调用子组件的方法,可以在子组件中加上ref,然后通过this.$refs.ref.method调用(https://www.cnblogs.com/jin-zhe/p/9523029.html)
Vue中子组件调用父组件的方法,这里有三种方法提供参考:
1:直接在子组件中通过this.$parent.event来调用父组件的方法
2:在子组件里用$emit向父组件触发一个事件,父组件监听这个事件
3:父组件把方法传入子组件中,在子组件里直接调用这个方法
https://www.cnblogs.com/jin-zhe/p/9523782.html)

@qq-radio
Copy link

$refs也行

@hyj443
Copy link

hyj443 commented Oct 19, 2021

子组件访问父组件:$parent

但少用 $parent 和 $children

它们主要用来应急,更推荐用 props 和 events 实现父子组件通信

如果子组件是公共组件,到处用,你在当前这个父组件下用没问题,但在别的地方用这个子组件,你还需要 $parent 吗?$parent 有对应的属性或方法吗?很容易产生bug,不利于调试。

所以这种写法是不被推荐的,官方文档有写。

@Azenmy
Copy link

Azenmy commented Oct 22, 2021

this.$emit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
vue vue
Projects
None yet
Development

No branches or pull requests

8 participants