我假设在Angular-cli tree-shaking exclude component from removal的问题是非常相似,但我似乎无法得到任何东西。Angular2(CLI)树抖动删除动态创建的NgModule
本质上,我有一个动态元件工厂,如How can I use/create dynamic template to compile dynamic Component with Angular 2.0?中所述。
当我使用最新的非生产设置的Angular CLI构建它时,它一切正常。但是,一旦我使用生产环境我立即得到在浏览器下面的错误跟踪尝试加载已动态创建内容的页面时:
EXCEPTION: No NgModule metadata found for 'e'.
ORIGINAL STACKTRACE:
main.dc05ae9….bundle.js:formatted:4731
Error: No NgModule metadata found for 'e'.
at f (vendor.c18e6df….bundle.js:formatted:76051)
at t.resolve (vendor.c18e6df….bundle.js:formatted:20624)
at t.getNgModuleMetadata (vendor.c18e6df….bundle.js:formatted:20169)
at t._loadModules (vendor.c18e6df….bundle.js:formatted:40474)
at t._compileModuleAndAllComponents (vendor.c18e6df….bundle.js:formatted:40462)
at t.compileModuleAndAllComponentsSync (vendor.c18e6df….bundle.js:formatted:40436)
at e.createComponentFactory (main.dc05ae9….bundle.js:formatted:4789)
这里是我的组件工厂类:
@Injectable()
export class DynamicTypeBuilder {
constructor() {
}
private _cacheOfFactories: {[templateKey: string]: ComponentFactory<any>} = {};
private compiler: Compiler = new JitCompilerFactory([{useDebug: false, useJit: true}]).createCompiler();
public createComponentFactory<COMPONENT_TYPE>(type: any, template: string, additionalModules: any[] = []): Observable<ComponentFactory<COMPONENT_TYPE>> {
let factory = this._cacheOfFactories[template];
if (factory) {
return Observable.of(factory);
}
// unknown template ... let's create a Type for it
let module = this.createComponentModule(type, additionalModules);
// compiles and adds the created factory to the cache
return Observable.of(this.compiler.compileModuleAndAllComponentsSync(module))
.map((moduleWithFactories: ModuleWithComponentFactories<COMPONENT_TYPE>) => {
factory = moduleWithFactories.componentFactories.find(value => value.componentType == type);
this._cacheOfFactories[template] = factory;
return factory;
});
}
protected createComponentModule(componentType: any, additionalModules: any[]): Type<any> {
@NgModule({
imports: [
FormsModule,
ReactiveFormsModule,
BrowserModule,
PipesModule,
...additionalModules
],
declarations: [
componentType
],
schemas:[CUSTOM_ELEMENTS_SCHEMA]
})
class RuntimeComponentModule {
}
return RuntimeComponentModule;
}
}
正被transpiled到
var _ = function() {
function e() {
this._cacheOfFactories = {},
this.compiler = new i.a([{
useDebug: !1,
useJit: !0
}]).createCompiler()
}
return e.prototype.createComponentFactory = function(e, t, n) {
var i = this;
var _ = this._cacheOfFactories[t];
if (_)
r.Observable.of(_);
var a = this.createComponentModule(e, n);
return r.Observable.of(this.compiler.compileModuleAndAllComponentsSync(a)).map(function(n) {
return _ = n.componentFactories.find(function(t) {
return t.componentType == e
}),
i._cacheOfFactories[t] = _,
_
})
}
,
e.prototype.createComponentModule = function(e, t) {
var n = function() {
function e() {}
return e
}();
return n
}
,
e.ctorParameters = function() {
return []
}
,
e
}()
在错误消息中的“E”是函数e()
从createComponentModule
WHI ch,正如你所看到的,尽管应该包含@NgModule
内容,但它仍然是空的。
如何动态创建新的NgModule并仍然使用Angular CLI的生产模式?
版本:
Angular2:2.4.8
角CLI:1.0.0-beta.32.3
打字稿:2.1.6
恐怕它不能正常工作。代码编译得很好,并且包含在经过转换的JS中,但是,现在有一个新问题。 Angular在新的'NgModule'中找不到任何自定义导入的模块。所以在上面的例子'FormsModule'中,可以找到'ReactiveFormsModule'和'BrowserModule',但是'PipesModule'不能。错误在'CompileMetadataResolver.getNgModuleMetadata'中。 – Sebastian
其实我有同样的问题。这似乎在JIT编译根本不编译装饰器。不仅是'RuntimeComponentModule'。如果你读过编译过的'main.js',它不包含'PipesModule'的元数据。我会继续这个tmr。 Thx告诉我角模块很好。 –
它现在正在工作jit + aot。我正在使用它在v4中使用 –