I am calling router.navigate
on same page with some query string parameters. In this case, ngOnInit()
does not call. Is it by default or do I need to add anything else ?
相关问题
- Angular RxJS mergeMap types
- npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fs
- How to update placeholder text in ng2-smart-table?
- How to instantiate Http service in main.ts manuall
- Angular: ngc or tsc?
相关文章
- angular脚手架在ie9+下兼容问题
- angular 前端项目 build 报错 "Cannot find module 'le
- Angular Material Stepper causes mat-formfield to v
- After upgrade to Angular 9 cannot find variable in
- is there any difference between import { Observabl
- Suppress “Circular dependency detected” suppress w
- How can you get current positional information abo
- Angular material table not showing data
NgOnInit
would be called once when an instance is created. For the same instanceNgOnInit
won't be called again. In order to call it it is necessary to destroy the created instance.You could adjust the reuseStrategy on the Router.
Consider moving the code you had in ngOnInit into ngAfterViewInit. The latter seems to be called on router navigation and should help you in this case.
You can inject the
ActivatedRoute
and subscribe toparams
The router only destroys and recreates the component when it navigates to a different route. When only route params or query params are updated but the route is the same, the component won't be destroyed and recreated.
An alternative way to force the component to be recreated is to use a custom reuse strategy. See also Angular2 router 2.0.0 not reloading components when same url loaded with different parameters? (there doesn't seem to be much information available yet how to implement it)
Maybe you actually need reloading page? This is my solution: I've changed my @NgModule (in app-routing.module.ts file in my case) :
This problem is likely coming from that fact that you are not terminating your subscriptions using ngOnDestroy. Here is how to get'ter done.
Bring in the following rxjs subscription import.
import { Subscription } from 'rxjs/Subscription';
Add OnDestory to your Angular Core Import.
import { Component, OnDestroy, OnInit } from '@angular/core';
Add OnDestory to your export class.
export class DisplayComponent implements OnInit, OnDestroy {
Create a object property with a value of Subscription from rxjs under your export class for each subscription on the component.
myVariable: Subscription;
Set the value of your subscription to MyVariable: Subscriptions.
this.myVariable = this.rmanagerService.getRPDoc(books[i].books.id).subscribe(value => {});
Then right below ngOninit place the ngOnDestory() life cycle hook and put in your unsubscribe statement for your subscription. If you have multiple, add more
ngOnDestroy() { this.myVariable.unsubscribe(); }