【发布时间】:2017-07-21 02:23:32
【问题描述】:
我认为Angular-cli tree-shaking exclude component from removal 的问题非常相似,但我似乎无法从中得到什么。
基本上我有一个动态组件工厂,如How can I use/create dynamic template to compile dynamic Component with Angular 2.0? 中所述。
当我使用带有非生产设置的最新 Angular CLI 构建它时,一切正常。但是,一旦我使用生产设置,当我尝试加载具有动态创建内容的页面时,我会立即在浏览器中看到以下错误跟踪:
异常:未找到“e”的 NgModule 元数据。
原始堆栈跟踪:
main.dc05ae9….bundle.js:formatted:4731
错误:没有 NgModule 元数据 为“e”找到。
在 f (vendor.c18e6df….bundle.js:formatted:76051)
在 t.resolve (vendor.c18e6df….bundle.js:formatted:20624)
在 t.getNgModuleMetadata (vendor.c18e6df….bundle.js:formatted:20169)
在 t._loadModules (vendor.c18e6df….bundle.js:formatted:40474)
在 t._compileModuleAndAllComponents (vendor.c18e6df….bundle.js:formatted:40462)
在 t.compileModuleAndAllComponentsSync (vendor.c18e6df….bundle.js:formatted:40436)
在 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;
}
}
正在被转译为
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”是来自createComponentModule 的函数e(),正如您所见,它是空的,即使它应该包含@NgModule 内容。
如何动态创建一个新的 NgModule 并且仍然使用 Angular CLI 的生产模式?
版本:
Angular2:2.4.8
Angular CLI:1.0.0-beta.32.3
打字稿:2.1.6
【问题讨论】:
-
请更新您的问题,因为它需要一些清晰度
标签: angular webpack angular-cli tree-shaking