我需要在 Angular 中动态创建路由和组件。我有这样的代码块来从声明属性中获取模块中定义的组件;
import { ɵReflectionCapabilities as ReflectionCapabilities } from "@angular/core";
routeFactory(types: any[]) {
//...
let annotations,
declarations: any[] = [];
for (let type of types) {
// Here annotations returns [] in AOT Prod Mode
// When prod mode is disabled it returns DecoratorFactory
annotations = refCap.annotations(type)[0];
declarations = declarations.concat(annotations.declarations);
}
}
我在 routeProvider 中使用此功能,如下所示;
export function routeProvider(...types: any[]) {
return {
ngModule: RouterModule,
providers: [
[
{
provide: ROUTES,
multi: true,
useFactory: routeFactory(types)
}
]
]
};
}
我的实际问题是使用此类设置进行编译时无法获得注释;
"production": {
"aot": true,
"buildOptimizer": true,
"optimization": true,
...
}
如果我禁用优化,那么它在 AOT 和 JIT 中都可以正常工作。按我的预期refCap.annotations(type)[0];
返回。[DecoratorFactory]
我相信这是因为 TerserPlugin 删除了不必要的功能/定义。我找到了一些帖子,但无法获得任何帮助;
如何获取 Angular 2 中当前模块的元数据?
从 Angular 2 组件中访问“选择器”
所以我想知道是否有任何可能性(hacky 或适当的)使用 AOT prod mod 中的 ReflectionCapabilities 访问注释及其声明。
角度版本:10.2.3