简体   繁体   English

Angular4路由parent> child:导航回父级时运行的挂钩?

[英]Angular4 routing parent > child : Hook that runs when navigating back to parent?

Say I have two components and their respective routes: 假设我有两个组成部分及其各自的路线:

  • ParentComponent /parent ParentComponent /parent
  • ChildComponent /parent/child ChildComponent /parent/child

I'm looking for a way to trigger a method on the parent as the router is told to navigate to "../" from the child view. 我正在寻找一种在父级上触发方法的方法,因为路由器被告知从子级视图导航到“ ../”。

I look for an equivalent of Ionic's hook "viewDidAppear": 我在寻找等效于Ionic的钩子“ viewDidAppear”:

  • it's not initialized as it was still in the nav stack 它尚未初始化,因为它仍在导航堆栈中
  • it's just getting foreground again and some refresh should occur 它只是再次成为前景,应该进行一些刷新

Thanks for your attention, 感谢您的关注,

Are they parent and child components or parent and child routes ? 它们是父级和子级组件还是父级和子级路由

When they are parent and child components , then the child component's selector is defined within the parent component's template. 当它们是父组件和子组件时 ,则在父组件的模板内定义子组件的选择器。 When that is the case, you can use @Output to communicate between the parent and the child. 在这种情况下,可以使用@Output在父级和子级之间进行通信。

If they are parent and child routes , then the child component's template appears in the parent component's router outlet. 如果它们是父路由和子路由 ,则子组件的模板将出现在父组件的路由器出口中。 In that case you can watch the route parameters with code like this: 在这种情况下,您可以使用以下代码观看路线参数:

this.route.params.subscribe(param => console.log(param));

Add this to the ngOnInit of the parent component to start watching the events. 将其添加到父组件的ngOnInit中以开始监视事件。 Then, instead of logging as I've shown here, you can trigger the desired method. 然后,您可以触发所需的方法,而不是像我在此处显示的那样进行记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM