导航到同一路线时如何刷新组件?

时间:2017-11-16 12:51:14

标签: angular

我有这个:

  this.activatedRoute.queryParams.subscribe((params: Params) => {
            if (localStorage.getItem('queryParams')) {
                this.queryParams = JSON.parse(localStorage.getItem('queryParams'));
                if (this.queryParams[Object.keys(this.queryParams)[0]]) {
                    this.procesId = this.queryParams[Object.keys(this.queryParams)[0]];
                }
            }
})

当我点击相同的路线时,我想要查询queryParams。任何建议我怎么能这样做?我有菜单,所以当我点击相同的链接,我想重新加载其他组件,我在queryParams中做一些事情。但是现在只有当路线改变时它才会触发。

1 个答案:

答案 0 :(得分:0)

如果要在导航到另一个组件时重新加载组件,可以这样做:

第一个组件:

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

export class ComponentOne implements OnInit {
     constructor() {//some stuff here}

     ngOnInit() {
          //here the code you want to trigger by nagivating to the second component
     }
}

第二部分:

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

    export class ComponentTwo implements OnInit {
         constructor(private firstComponent: ComponentOne) {} //dependency injection

         ngOnInit() {
              this.firstComponent.ngOnInit(); //this will call the ngOnInit from the first component
         }
    }