我指的是How can I use/create dynamic template to compile dynamic Component with Angular 2.0?
并在其中使用Jitcompiler
type.service.ts
:
import { Component, ComponentFactory, NgModule, Input, Injectable} from
'@angular/core';
import { JitCompiler } from '@angular/compiler';
import { PartsModule } from '../parts/parts.module';
import * as _ from "lodash"
export interface IHaveDynamicData {
entity: any;
}
@Injectable()
export class DynamicTypeBuilder {
// wee need Dynamic component builder
constructor(
protected compiler: JitCompiler
) {}
}
此内容将进一步导入并在提供程序下提及
dynamic.module
:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
// parts module
import { PartsModule } from '../parts/parts.module';
// detail stuff
import { DynamicDetail } from './detail.view';
import { DynamicTypeBuilder } from './type.service';
import { DynamicTemplateBuilder } from './template.service';
@NgModule({
imports: [ PartsModule ],
declarations: [ DynamicDetail ],
exports: [ DynamicDetail]
})
export class DynamicModule {
static forRoot()
{
return {
ngModule: DynamicModule,
providers: [ // singletons across the whole app
DynamicTemplateBuilder,
DynamicTypeBuilder
],
};
}
}
然后将DynamicModule导入app.module
import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-
browser/animations';
import { FormsModule } from '@angular/forms';
import { ReactiveFormsModule } from '@angular/forms';
import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';
import { AppComponent } from '@/app.component';
import { routing } from '@/router';
import {
JwtInterceptor,
ErrorInterceptor,
HttpCancelInterceptor,
} from '@shared/_helpers';
import { SharedModule } from '@/shared/shared.module'
import { LoginComponent } from '@/pages/fs-login';
import { DynamicModule } from './dynamic/dynamic.module';
@NgModule({
imports: [
BrowserModule,
BrowserAnimationsModule,
FormsModule,
ReactiveFormsModule,
HttpClientModule,
SharedModule,
routing,
DynamicModule.forRoot()
],
declarations: [
AppComponent
],
providers: [
{ provide: HTTP_INTERCEPTORS, useClass: JwtInterceptor, multi: true },
{ provide: HTTP_INTERCEPTORS, useClass: ErrorInterceptor, multi: true },
{ provide: HTTP_INTERCEPTORS, useClass: HttpCancelInterceptor, multi:
true }
],
bootstrap: [AppComponent],
schemas: [
CUSTOM_ELEMENTS_SCHEMA
],
exports: [
SharedModule
]
})
export class AppModule { }
在运行应用程序时出现错误
ERROR Error: Uncaught (in promise): Error: StaticInjectorError(AppModule)
[DynamicTypeBuilder -> JitCompiler]:
StaticInjectorError(Platform: core)[DynamicTypeBuilder -> JitCompiler]:
NullInjectorError: No provider for JitCompiler!
Error: StaticInjectorError(AppModule)[DynamicTypeBuilder -> JitCompiler]:
StaticInjectorError(Platform: core)[DynamicTypeBuilder -> JitCompiler]:
NullInjectorError: No provider for JitCompiler!