blb*_*ker 5 javascript typescript angular
前言:我的 URL 策略的一部分有一个可选参数。也就是说,URL 路径可以以区域代码和语言代码开头,也可以仅以语言代码开头。然后,这些代码将按照实际的应用程序路线进行处理。
/us/eng --> Home page
/us/eng/about-us --> About Us page
/eng/about-us --> About Us page
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用 UrlMatcher 来完成 URL 中的可选区域代码。
我的问题是,主页始终显示。它从不显示“关于我们”页面或任何其他子路线。
部分app.routes.ts
export function baseRouteMatcher(url: UrlSegment[]) {
const posParams: { [key: string]: UrlSegment } = {};
const consumed: UrlSegment[] = url;
if (url[0].path.length === 2 && url[1].path.length === 3) {
posParams.region = url[0];
posParams.lang = url[1];
} else if(url[0].path.length === 3) {
posParams.region = new UrlSegment('world', {});
posParams.lang = url[0];
}
return ({consumed, posParams});
}
export const appRoute = {
name: 'app',
matcher: baseRouteMatcher,
children: [
{ path: 'terms-of-service', component: ContentComponent },
{ path: 'privacy-policy', component: ContentComponent },
{ path: 'about-us', loadChildren: './about-us/about-us.module#AboutUsModule' },
{ path: '', component: HomeComponent },
]
};
Run Code Online (Sandbox Code Playgroud)
我很可能完全误解了 UrlMatcher 的功能。很难找到复杂的例子。任何帮助表示赞赏!
这是真的。我不明白 UrlMatcher 是如何工作的。发生了两件事:
HomeComponent每次都得到。这是工作代码:
export function baseRouteMatcher(segments) {
// If we don't have any segments
if (!segments[0]) {
return null;
}
// If we only have a language, consume just the first segment
// Else if we have a region and a language, consume first two segments
if (segments[0].path.length === 3) {
return { consumed: segments.slice(0, 1), posParams: { region: 'world', lang: segments[0].path } };
} else if (segments[0].path.length === 2 && segments[1].path.length === 3) {
return { consumed: segments.slice(0, 2), posParams: { region: segments[0].path, lang: segments[1].path } };
}
// The segments don't contain a region or language, don't consume anything
return null;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4279 次 |
| 最近记录: |