Ais*_*shu 2 components ionic-framework angular
我正在尝试动态添加组件ngOnInit。创建组件时,出现此错误。
"Uncaught (in promise): Error: Template parse errors: 'ion-title' is not a
known element"
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
TS:
import { Component,Input, OnInit, Compiler, Injector, VERSION, ViewChild,
NgModule, NgModuleRef, ViewContainerRef, AfterViewInit,
ComponentFactoryResolver } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { ViewController } from 'ionic-angular';
@Component({
selector: 'page-cashbalancemodal_rpt',
template: '<ng-container #dynamicTemplate></ng-container>',
})
export class Cashbalancemodal_rptPage {
@ViewChild('dynamicTemplate', {read: ViewContainerRef})
viewContainerRef;
constructor(){.......}
ngOnInit() {
let template = this.navParams.get("modaltemplate");
let myTemplateUrl = "assets/templates/"+template+".html";
const tmpCmp = Component({
templateUrl: myTemplateUrl
})(class {});
const tmpModule = NgModule({
declarations: [tmpCmp],
entryComponents: [tmpCmp],
})(class {});
this._compiler.compileModuleAndAllComponentsAsync(tmpModule)
.then((factories) => {
const f = factories.componentFactories[0];
const cmpRef = this.viewContainerRef.createComponent(f);
cmpRef.instance.name = 'dynamic';
})
}
}
Run Code Online (Sandbox Code Playgroud)
我遇到了许多解决方案。最常用的解决方案之一是在app.module.ts中添加“ IonicModule”。我也是。但是不能摆脱这个错误。
app.module.ts
import { IonicApp, IonicErrorHandler, IonicModule } from 'ionic-angular';
@NgModule({
imports: [
BrowserModule,
IonicStorageModule.forRoot(),
HttpModule,
IonicModule.forRoot(MyApp),
ComponentsModule,
],
})
Run Code Online (Sandbox Code Playgroud)
请帮助解决此错误。
由于您使用的离子组分位于tmpCmp中tmpModule,
您需要导入IonicModule该模块。
const tmpModule = NgModule({
declarations: [tmpCmp],
imports:[IonicModule],
entryComponents: [tmpCmp],
})(class {});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2006 次 |
| 最近记录: |