简体   繁体   English

如何用routerlink和id刷新angular中的页面

[英]How to refresh the page in angular with routerlink and id

I have an angular application In that I have some navigation of routerlinks我有一个 angular 应用程序,其中有一些路由器链接导航

My requirement is if the user page is already open with some Id,and if the condition satisfies in then it has to navigate to that particular ID user page from the existing user page with ID.我的要求是如果用户页面已经打开并带有一些 ID,并且如果条件满足则它必须从带有 ID 的现有用户页面导航到该特定 ID 用户页面。

.component.ts .component.ts

  if(userID == 0){
             this.router.navigate(['./user/' + userID]);
           }
           else{
            this.router.navigate(['./details']);
            
           }

From the above code if I am already in the user page with id then condition satisfies in if condition then we have to navigate to that particular user page with that ID.从上面的代码中,如果我已经在具有 id 的用户页面中,则条件满足,如果条件满足,则我们必须导航到具有该 ID 的特定用户页面。

Can anyone help me on the same任何人都可以帮助我吗

In order to refresh the current route, in your route declaration you have to do:为了刷新当前路线,您必须在路线声明中执行以下操作:

RouterModule.forRoot(appRoutes, {
  // ..
  onSameUrlNavigation: 'reload',
  // ..
})

Instead, to navigate to another route with params you have to:相反,要使用参数导航到另一条路线,您必须:

1 - Redefine your route 1 - 重新定义你的路线

{path: 'user/:userID', component: MyComponent }

2 - Navigate to page 2 - 导航到页面

this.router.navigate(["user", userID]);

3 - Get the param from your page 3 - 从您的页面获取参数

import { ActivatedRoute } from '@angular/router';

private userID: string;

constructor(private readonly activatedRoute: ActivatedRoute) {}

ngOnInit() {
    this.activatedRoute.params.subscribe((params: Params) => this.userID = params['userID']);
}

For more information see Angular router and ActivatedRoute有关详细信息,请参阅Angular 路由器ActivatedRoute

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

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