简体   繁体   English

从子级到父级不调用Angular 6 ngOninit

[英]Angular 6 ngOninit not called going from child to parent

Consider the following setup: 请考虑以下设置:

{ path: 'module1', component: Module1Component, children: [
    { path: 'component1', component: Component1Component, children: [
        { path: 'component2', component: Component2Component }
    ] }
] },

So based on the above: 因此,基于以上内容:

  • /module1 loads Module1Component / module1加载Module1Component
  • /module1/component1 loads Component1Component / module1 / component1加载Component1Component
  • /module1/component1/component2 loads Component2Component / module1 / component1 / component2加载Component2Component

Also

  • Module1Component is the parent of Component1Component Module1ComponentComponent1Component的父级
  • Component1Component is the parent of Component2Component Component1ComponentComponent2Component的父

Module1Component Code Module1组件代码

import { Component, OnInit } from "@angular/core";

@Component({
    selector: 'app-module1-component',
    templateUrl: './module1.component.html',
    styleUrls: ['./module1.component.css']
})
export class Module1Component implements OnInit {

    constructor() {}

    ngOnInit() {
        console.log('Module1Component');
    }
}

Component1Component Code Component1组件代码

import { Component, OnInit } from '@angular/core';

@Component({
    selector: 'app-component1',
    templateUrl: './component1.component.html',
    styleUrls: ['./component1.component.css']
})
export class Component1Component implements OnInit {

    constructor() { }

    ngOnInit() {
        console.log('Component1Component');
    }

}

Component2Component Code Component2组件代码

import { Component, OnInit } from '@angular/core';

@Component({
    selector: 'app-component2',
    templateUrl: './component2.component.html',
    styleUrls: ['./component2.component.css']
})
export class Component2Component implements OnInit {

    constructor() { }

    ngOnInit() {
        console.log('Component2Component');
    }

}

ISSUE 问题

Given these links: 鉴于这些链接:

<a [routerLink]="['/module1']">/module1</a>
<a [routerLink]="['/module1/component1']">/module1/component1</a>
<a [routerLink]="['/module1/component1/component2']">/module1/component1/component2</a>
  • When you are on /module1/component1/component2 and you go to /module1/component1 using the routerLink above the ngOnInit of Component1Component is not called . 当您位于/ module1 / component1 / component2上,并且未使用Component1Component的ngOnInit上方的routerLink转到/ module1 / component1时

  • When you are on /module1/component1 and go to /module1 using the routerLink above the ngOnInit of Module1Component is not called . 当您在/ module1 / component1上,并使用不调用Module1Component的ngOnInit上方的routerLink转到/ module1时

Conclusion : The ngOnInit going from a child to a parent using routerLink is not called, why and how can it be configured to do so ? 结论 :不会调用使用routerLink从子级到父级的ngOnInit,为什么以及如何对其进行配置?

NOTE : The same behavior occurs when using router.navigate or using the back button in the browser . 注意 :使用router.navigate或使用浏览器中的后退”按钮时, 也会发生相同的行为。

It is expected behavior that the ngOnInit is not called when navigating to the same component with different parameters. 导航到具有不同参数的相同组件时,不会调用ngOnInit的预期行为。

But you should not call ngOnInit manually. 但是您不应该手动调用ngOnInit。

The subscription will get notified each time the route parameters change. 每次路由参数更改时,订阅都会收到通知。 So code within the subscribe method will execute each time. 因此,subscribe方法中的代码将每次执行。 You don't need to call ngOnInit. 您无需调用ngOnInit。

Try it by moving your console.log within the route.params.subscribe. 通过在route.params.subscribe中移动console.log进行尝试。

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

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