[英]Angular 2 Routing with a specific url
i'm currently working on an angular app which is opened on a particular url: 我目前正在开发在特定网址上打开的角度应用程序:
localhost:3000/?param1=abc,def¶m2=abcdefghi
Now i want to implement angular routing . 现在我要实现角度路由。 So i have 2 routes :
1. mainComponent 2.dataComponent
所以我有2条路线:
1. mainComponent 2.dataComponent
in my index.html ,have set the as : 在我的index.html中,将设置为:
<base href="/">
After configuring the app to use routing , my app opens with only localhost:3000 , and even after adding the param1,param2 to the url , it is getting redirected to localhost:3000 将应用程序配置为使用路由后,我的应用程序仅以localhost:3000打开,即使将param1,param2添加到url之后,它也将重定向到localhost:3000
How can i solve this ? 我该如何解决? as the parameters are passed only on opening the link and it is dynamic
因为仅在打开链接时传递参数,所以它是动态的
app.module.ts: app.module.ts:
const appRoutes: Routes = [
{ path: 'mainComponent', component: MainComponent },
{ path: 'dataComponent', component: DataComponent },
{path : '' , component:MainComponent}
];
app.component.html: app.component.html:
<nav>
<a routerLink="/mainComponent" routerLinkActive="active">Main</a>
<a routerLink="/dataComponent" routerLinkActive="active">Data</a>
</nav>
<router-outlet></router-outlet>
You need to change your routes as following 您需要按以下方式更改路线
const appRoutes: Routes = [
{path : '' ,redirectTo: 'mainComponent', pathMatch: 'full'},
{ path: 'mainComponent', component: MainComponent },
{ path: 'dataComponent', component: DataComponent },
];
And better way to provide links 提供链接的更好方法
<nav>
<a [routerLink]="['/mainComponent']" routerLinkActive="active">Main</a>
<a [routerLink]="['/dataComponent']" [queryParams]="{ param: "paramValue" } routerLinkActive="active">Data</a>
</nav>
To receive parameters you need to do following in your component constructor 要接收参数,您需要在组件构造函数中执行以下操作
constructor(private activatedRoute: ActivatedRoute){
this.type = activatedRoute.snapshot.params['type'];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.