[英]What is the replacement for the Angular Compiler in v14?
I am trying to upgrade to Angular 14. My current app uses the Compiler from '@angular/core' to compile angular templates at runtime.我正在尝试升级到 Angular 14。我当前的应用程序使用“@angular/core”中的编译器在运行时编译 angular 模板。 In order to use it I had to include the --aot=false flag when I built.
为了使用它,我必须在构建时包含 --aot=false 标志。 I know has been deprecated for a few versions.
我知道有几个版本已被弃用。
After upgrading, I am getting errors trying to build saying "template must be a string".升级后,我在尝试构建时遇到错误,提示“模板必须是字符串”。 This is in response to me attempting to create an angular Component at runtime.
这是对我试图在运行时创建一个 angular 组件的回应。
Just to be clear, what I mean by an angular template string would be something like明确一点,我所说的 angular 模板字符串的意思是
let angularTemplateString = 'Hello {{ firstNameFromPassedInContext }}';
They get much more complex, but that's the gist of it.它们变得更加复杂,但这就是它的要点。
I suspect that there is still a way to compile angular templates at runtime in v14.我怀疑在 v14 中仍然有办法在运行时编译 angular 模板。 Does anyone know how to do this, or if there is a better way to do this that is compatible with v14?
有谁知道如何做到这一点,或者是否有更好的方法与 v14 兼容?
Is something like this what you are looking for?您正在寻找这样的东西吗?
@Component({
selector: 'hello',
template: '<div #container></div>',
})
export class HelloComponent implements AfterViewInit {
@ViewChild('container', { read: ViewContainerRef })
container: ViewContainerRef;
constructor(private injector: Injector) {}
ngAfterViewInit() {
// Define the component using Component decorator.
const component = Component({
selector: 'test',
template: '<div>This is the dynamic template. Test value: {{test}}</div>',
styles: [':host {color: red}'],
})(
class {
test = 'some value';
}
);
// Define the module using NgModule decorator.
const module = NgModule({ declarations: [component] })(class {});
const componentRef = this.container.createComponent(component, {
injector: this.injector,
ngModuleRef: createNgModuleRef(module, this.injector),
});
setTimeout(() => (componentRef.instance.test = 'some other value'), 2000);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.