How to router.navigate to same route in Angular 4 and catch the same event?

前端 未结 4 486
情深已故
情深已故 2021-01-02 08:35

Okay, I have two use cases for my question here:

  1. I\'m working on an application which has a /en/register route. It all works good when I\'m at t

相关标签:
4条回答
  • 2021-01-02 09:07

    For case 1,

    Why do you navigate to a new route just to open a modal. Just do it being at the same route with a function call. If you want to move to a new route then you can again call this.router.navigate("/") in modal close event.

    For case 2, Hope this will work.

    currentSearchQuery: string;
    
    ngOnInit() {
      this.route.paramMap
        .switchMap((params: ParamMap) => {
            this.currentSearchQuery = params.get('searchQuery');
            updateMarkers();
          });
    
    }
    
    0 讨论(0)
  • 2021-01-02 09:10

    i use this workaround

    this.router.navigate(['path']).then(()=>  {window.location.reload();});
    
    0 讨论(0)
  • 2021-01-02 09:14

    Just add dummy parameter at the end of the route /en/register/jk2h4-42hj-234n2-234dfg or query parameter like /en/register?val=jk2h4-42hj-234n2-234dfg

    change the parameter value when calling the same route. So browser knows that URL change and Angualr component start to work from full life cycle.

    0 讨论(0)
  • 2021-01-02 09:17

    When I needed to "reload" the current component's constructor and ngOnInit functions, the only solution I found was kind of a workaround:

    I used the fact that "this.router.navigate" returns a promise. So I navigated somewhere else, and returned. It's a bit ugly but it works and the UI is not affected:

    this.router.navigate(..[somewhere else]..)
        .then(()=>{this.router.navigate(..back..)})
    

    Hope it helps.

    0 讨论(0)
提交回复
热议问题