UI: Improve dynamic components compilation - compile component in place to correctly handle template compile errors.
This commit is contained in:
parent
bbc3e904c8
commit
0d7a6bbaf3
@ -22,7 +22,7 @@ import {
|
||||
NgModule,
|
||||
NgModuleRef,
|
||||
OnDestroy,
|
||||
Type,
|
||||
Type, ɵNG_COMP_DEF,
|
||||
ɵresetCompiledComponents
|
||||
} from '@angular/core';
|
||||
import { from, Observable, of } from 'rxjs';
|
||||
@ -64,7 +64,6 @@ export class DynamicComponentFactoryService {
|
||||
template: string,
|
||||
modules?: Type<any>[],
|
||||
preserveWhitespaces?: boolean,
|
||||
compileAttempt = 1,
|
||||
styles?: string[]): Observable<DynamicComponentData<T>> {
|
||||
return from(import('@angular/compiler')).pipe(
|
||||
mergeMap(() => {
|
||||
@ -78,32 +77,26 @@ export class DynamicComponentFactoryService {
|
||||
declarations: [comp],
|
||||
imports: moduleImports
|
||||
})(class DynamicComponentInstanceModule extends DynamicComponentModule {});
|
||||
|
||||
const module = this.compiler.compileModuleSync(dynamicComponentInstanceModule);
|
||||
let moduleRef: NgModuleRef<any>;
|
||||
try {
|
||||
const module = this.compiler.compileModuleSync(dynamicComponentInstanceModule);
|
||||
let moduleRef: NgModuleRef<any>;
|
||||
try {
|
||||
moduleRef = module.create(this.injector);
|
||||
} catch (e) {
|
||||
this.compiler.clearCacheFor(module.moduleType);
|
||||
throw e;
|
||||
}
|
||||
this.dynamicComponentModulesMap.set(comp, {
|
||||
moduleRef,
|
||||
moduleType: module.moduleType
|
||||
});
|
||||
return of( {
|
||||
componentType: comp,
|
||||
componentModuleRef: moduleRef
|
||||
});
|
||||
} catch (error) {
|
||||
if (compileAttempt === 1) {
|
||||
ɵresetCompiledComponents();
|
||||
return this.createDynamicComponent(componentType, template, modules, preserveWhitespaces, ++compileAttempt, styles);
|
||||
} else {
|
||||
console.error(error);
|
||||
throw error;
|
||||
}
|
||||
moduleRef = module.create(this.injector);
|
||||
// eslint-disable-next-line
|
||||
comp[ɵNG_COMP_DEF];
|
||||
} catch (e) {
|
||||
this.compiler.clearCacheFor(module.moduleType);
|
||||
ɵresetCompiledComponents();
|
||||
throw e;
|
||||
}
|
||||
this.dynamicComponentModulesMap.set(comp, {
|
||||
moduleRef,
|
||||
moduleType: module.moduleType
|
||||
});
|
||||
return of( {
|
||||
componentType: comp,
|
||||
componentModuleRef: moduleRef
|
||||
});
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
@ -160,7 +160,7 @@ export class TbMarkdownComponent implements OnChanges {
|
||||
},
|
||||
template,
|
||||
compileModules,
|
||||
true, 1, styles
|
||||
true, styles
|
||||
).subscribe((componentData) => {
|
||||
this.tbMarkdownInstanceComponentType = componentData.componentType;
|
||||
const injector: Injector = Injector.create({providers: [], parent: this.markdownContainer.injector});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user