装饰器不支持 'AppRoutingModule' 函数表达式的模板编译期间出错
Error during template compile of 'AppRoutingModule' Function expressions are not supported in decorators
我正在使用 "CaseInsensitiveMatcher" 作为 Alireza 提出。问题是在尝试进行产品构建时,出现以下错误
"'urlMatch' references 'CaseInsensitiveMatcher'
'CaseInsensitiveMatcher' contains the error at src\app\providers\caseInsensitiveMatcher.ts(4,10)
Consider changing the function expression into an exported function"
来自应用程序路由模块的示例代码
export function urlMatch(str: string) {
return CaseInsensitiveMatcher(str).apply(this, arguments);
}
const routes: Routes = [
{ matcher: urlMatch('en-ie'), component: HomepageComponent },
{ matcher: urlMatch('en-gb'), component: HomepageComponent },
]
@NgModule({
imports: [RouterModule.forRoot(routes),SharedModule, AccountModule],
exports: [RouterModule, SharedModule, AccountModule]
})
export class AppRoutingModule { }
CaseInsensitveMatcher 代码
import {Route, UrlSegment, UrlSegmentGroup} from '@angular/router';
export function CaseInsensitiveMatcher(url: string) {
return function(
segments: UrlSegment[],
segmentGroup: UrlSegmentGroup,
route: Route
) {
const matchSegments = url.split('/');
if (
matchSegments.length > segments.length ||
(matchSegments.length !== segments.length && route.pathMatch === 'full')
) {
return null;
}
const consumed: UrlSegment[] = [];
const posParams: {[name: string]: UrlSegment} = {};
for (let index = 0; index < matchSegments.length; ++index) {
const segment = segments[index].toString().toLowerCase();
const matchSegment = matchSegments[index];
if (matchSegment.startsWith(':')) {
posParams[matchSegment.slice(1)] = segments[index];
consumed.push(segments[index]);
} else if (segment.toLowerCase() === matchSegment.toLowerCase()) {
consumed.push(segments[index]);
} else {
return null;
}
}
return { consumed, posParams };
};
}
如果有人能告诉我我做错了什么,我将不胜感激?甚至如何解决它。无论哪种方式,感谢您花时间阅读
Link To Orginal for Context
CaseInsensitveMatcher returns 一个匿名函数,但不幸的是 Angular 不允许在 AOT 中使用这样的动态代码。您可以做的是修改您的解决方案,以便您只使用命名的导出函数而不涉及匿名函数。
我正在使用 "CaseInsensitiveMatcher" 作为 Alireza 提出。问题是在尝试进行产品构建时,出现以下错误
"'urlMatch' references 'CaseInsensitiveMatcher' 'CaseInsensitiveMatcher' contains the error at src\app\providers\caseInsensitiveMatcher.ts(4,10) Consider changing the function expression into an exported function"
来自应用程序路由模块的示例代码
export function urlMatch(str: string) {
return CaseInsensitiveMatcher(str).apply(this, arguments);
}
const routes: Routes = [
{ matcher: urlMatch('en-ie'), component: HomepageComponent },
{ matcher: urlMatch('en-gb'), component: HomepageComponent },
]
@NgModule({
imports: [RouterModule.forRoot(routes),SharedModule, AccountModule],
exports: [RouterModule, SharedModule, AccountModule]
})
export class AppRoutingModule { }
CaseInsensitveMatcher 代码
import {Route, UrlSegment, UrlSegmentGroup} from '@angular/router';
export function CaseInsensitiveMatcher(url: string) {
return function(
segments: UrlSegment[],
segmentGroup: UrlSegmentGroup,
route: Route
) {
const matchSegments = url.split('/');
if (
matchSegments.length > segments.length ||
(matchSegments.length !== segments.length && route.pathMatch === 'full')
) {
return null;
}
const consumed: UrlSegment[] = [];
const posParams: {[name: string]: UrlSegment} = {};
for (let index = 0; index < matchSegments.length; ++index) {
const segment = segments[index].toString().toLowerCase();
const matchSegment = matchSegments[index];
if (matchSegment.startsWith(':')) {
posParams[matchSegment.slice(1)] = segments[index];
consumed.push(segments[index]);
} else if (segment.toLowerCase() === matchSegment.toLowerCase()) {
consumed.push(segments[index]);
} else {
return null;
}
}
return { consumed, posParams };
};
}
如果有人能告诉我我做错了什么,我将不胜感激?甚至如何解决它。无论哪种方式,感谢您花时间阅读
Link To Orginal for Context
CaseInsensitveMatcher returns 一个匿名函数,但不幸的是 Angular 不允许在 AOT 中使用这样的动态代码。您可以做的是修改您的解决方案,以便您只使用命名的导出函数而不涉及匿名函数。