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

Day90:Vue父组件可以监听到子组件的生命周期吗?如果能请写出你的实现方法。 #138

Open
lgwebdream opened this issue Jul 6, 2020 · 2 comments
Labels
Vue teach_tag

Comments

@lgwebdream
Copy link
Owner

No description provided.

@lgwebdream
Copy link
Owner Author

lgwebdream commented Jul 6, 2020

答案

可以

1)实现方式一

比如有父组件 Parent 和子组件 Child,如果父组件监听到子组件挂载 mounted 就做一些逻辑处理,可以通过以下写法实现:

// Parent.vue
<Child @mounted="doSomething"/>
    
// Child.vue
mounted() {
  this.$emit("mounted");
}

2)实现方式二

以上需要手动通过 $emit 触发父组件的事件,更简单的方式可以在父组件引用子组件时通过 @hook 来监听即可,如下所示:

//  Parent.vue
<Child @hook:mounted="doSomething" ></Child>

doSomething() {
   console.log('父组件监听到 mounted 钩子函数 ...');
},
    
//  Child.vue
mounted(){
   console.log('子组件触发 mounted 钩子函数 ...');
},  

// 以上输出顺序为:
// 子组件触发 mounted 钩子函数 ...
// 父组件监听到 mounted 钩子函数 ...     

当然 @hook 方法不仅仅是可以监听 mounted,其它的生命周期事件,例如:created,updated 等都可以监听。

@Genzhen Genzhen added the Vue teach_tag label Jul 7, 2020
@luuman
Copy link

luuman commented Nov 1, 2021

  1. emit:子组件emit通知父组件
  2. hook:父组件@hook:mounted="doSomething"监听子组件
  3. eventbus

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

No branches or pull requests

3 participants