Router Navigate does not call ngOnInit when same page

前端 未结 12 1051
时光取名叫无心
时光取名叫无心 2020-11-28 03:20

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

12条回答
  •  甜味超标
    2020-11-28 03:51

    This problem is likely coming from that fact that you are not terminating your subscriptions using ngOnDestroy. Here is how to get'ter done.

    1. Bring in the following rxjs subscription import. import { Subscription } from 'rxjs/Subscription';

    2. Add OnDestory to your Angular Core Import. import { Component, OnDestroy, OnInit } from '@angular/core';

    3. Add OnDestory to your export class. export class DisplayComponent implements OnInit, OnDestroy {

    4. Create a object property with a value of Subscription from rxjs under your export class for each subscription on the component. myVariable: Subscription;

    5. Set the value of your subscription to MyVariable: Subscriptions. this.myVariable = this.rmanagerService.getRPDoc(books[i].books.id).subscribe(value => {});

    6. 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(); }

提交回复
热议问题