我想知道你们是否能帮助编写这段代码!因为我觉得我疯了
这是helpersmatcher.ts(4,12)
:
import { UrlMatcher , UrlSegment } from "@angular/router";
export function match(path: string): UrlMatcher {
return function matcher(url: UrlSegment[], group) {
console.log(path, url, group);
if (url.length > 0 && url[0].path === path) {
return { consumed: [url[0]] };
}
return null;
};
}
这是我在构建--prod 时遇到的错误
src\app\app.module.ts中的错误(124,30(:在"AppModule"的模板编译过程中出错
函数表达式在"rootRouterConfig"中的装饰器中不受支持
'rootRouterConfig'引用"match">
'match'包含src\app\shared\helpers\matcher处的错误。ts(4,12(
请考虑将函数表达式更改为导出的函数
但它可以编译并与ng构建和ng服务完美配合。
这是应用程序路径
export const rootRouterConfig: Routes = [
{ path: ':param', component: HomeComponent,
children: [
{ matcher: match('job-spec/:id'), component: JobSpecDetailsComponent },
{ path: '**', redirectTo: '404' },
]
},
];
这是app.module
@NgModule({
imports: [
....
RouterModule.forRoot(rootRouterConfig, {
useHash: false,
scrollPositionRestoration: 'enabled',
anchorScrolling: 'enabled',
}),
....
我无法测试它,但尝试以下操作:
export function match(url: UrlSegment[]): UrlMatcher {
if (url.length > 0 && url[0].path === path) {
return { consumed: [url[0]] };
}
return null;
}
并将其用作:
{ matcher: match, component: JobSpecDetailsComponent },
URL
有路径,但不确定您是否需要再次通过该路径。静态使用路径,看看它是否编译,然后计算出你的路径。