在Angular 2的路由中可以有一个可选的路由参数吗?我尝试了Angular 1。但是在routecconfig中收到如下错误:
“ORIGINAL EXCEPTION: Path /user/:id?”包含“?”,这在路由配置中是不允许的。
@RouteConfig([
{
path: '/user/:id?',
component: User,
as: 'User'
}])
在Angular 2的路由中可以有一个可选的路由参数吗?我尝试了Angular 1。但是在routecconfig中收到如下错误:
“ORIGINAL EXCEPTION: Path /user/:id?”包含“?”,这在路由配置中是不允许的。
@RouteConfig([
{
path: '/user/:id?',
component: User,
as: 'User'
}])
当前回答
{path: 'users', redirectTo: 'users/', pathMatch: 'full'},
{path: 'users/:userId', component: UserComponent}
这样,在添加参数时就不会重新呈现组件。
其他回答
通过路由将路由参数从一个组件发送到另一个组件有三种方式。但是首先要将这些库导入到与.ts文件相关的组件中,并在构造函数中进行定义
private route: ActivatedRoute
private router: Router
第一种方式:必需的路由参数
//Route Configuration
{path: 'user/:id', component: UserDetailComponent}
//Set Hyperlink/routerLink
<a [routerLink]="['/user', user.id]"></a>
//Requesting Url after click on hyperlink
http://localhost:4200/user/6
//Now you can read id value in navigated component
this.route.snapshot.paramMap.get('id');
第二种方式:可选路径参数
//Route Configuration
{path: 'user', component: UserDetailComponent}
//Set Hyperlink/routerLink
<a [routerLink]=['/user', {name: userName, status: true}]"></a>
//Requesting Url after click on hyperlink
http://localhost:4200/user;name:userNameValue;status:true
//Now you can read values in navigated component
this.route.snapshot.paramMap.get('userId');
this.route.snapshot.paramMap.get('userName');
第三种方式:可选路径参数
//Route Configuration
{path: 'user', component: UserDetailComponent}
//Set Hyperlink/routerLink
<a [routerLink]="['/user']" [queryParms]="{userId:'911', status:true}"></a>
//Requesting Url after click on hyperlink
http://localhost:4200/user?userId=911&status=true
//Now you can read values in navigated component
this.route.snapshot.paramMap.get('userId');
this.route.snapshot.paramMap.get('userName');
参考:https://qastack.mx/programming/44864303/send-data-through-routing-paths-in-angular
主细节视图也有同样的问题。主视图可以在没有:elementId参数的情况下显示,但仍然应该显示详细选择并在url中显示:elementId。
我是这样解决的:
const routes: Routes = [
{
path: '',
component: MasterDetailComponent,
children: [
{
path: ':elementId',
children: [
{
path: 'details',
component: DetailComponent
},
{
path: '',
redirectTo: 'details'
}
]
}
]
}
];
然后在MasterDetailComponent中(例如在ngOnInit方法中),你可以使用子路由获得:elementId:
const childRouteWithElementId = this.route.snapshot.children[0];
const elementIdFromUrl = childRouteWithElementId.params.elementId;
if (!!elementIdFromUrl ) {
// Do what you need to with the optional parameter
}
当然,你也可以在没有子路由的情况下做同样的事情,只在url的末尾有可选的elementId。
这里建议的答案,包括来自rerezz的接受答案,它建议添加多个路由条目。
然而,当路由表项之间发生变化时,即在带参数的路由表项和不带参数的路由表项之间,组件将被重新创建。
如果你想避免这种情况,你可以创建自己的路由匹配器来匹配这两个路由:
export function userPageMatcher(segments: UrlSegment[]): UrlMatchResult {
if (segments.length > 0 && segments[0].path === 'user') {
if (segments.length === 1) {
return {
consumed: segments,
posParams: {},
};
}
if (segments.length === 2) {
return {
consumed: segments,
posParams: { id: segments[1] },
};
}
return <UrlMatchResult>(null as any);
}
return <UrlMatchResult>(null as any);
}
然后在路由配置中使用matcher:
const routes: Routes = [
{
matcher: userPageMatcher,
component: User,
}
];
使用这个匹配器函数,您可以在不重新渲染组件的情况下获得理想的行为。当url。长度等于0,没有可选参数,url。长度等于1,有1个可选参数。Id -可选参数名称。
const routes: Routes = [
{
matcher: (segments) => {
if (segments.length <= 1) {
return {
consumed: segments,
posParams: {
id: new UrlSegment(segments[0]?.path || '', {}),
},
};
}
return null;
},
pathMatch: 'prefix',
component: UserComponent,
}]
你可以定义多个带参数和不带参数的路由:
@RouteConfig([
{ path: '/user/:id', component: User, name: 'User' },
{ path: '/user', component: User, name: 'Usernew' }
])
并在组件中处理可选参数:
constructor(params: RouteParams) {
var paramId = params.get("id");
if (paramId) {
...
}
}
参见相关的github问题:https://github.com/angular/angular/issues/3525