简体   繁体   English

v14 中 Angular 编译器的替代品是什么?

[英]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);
  }
}

Stackblitz 堆栈闪电战

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular 从 v12 迁移到 v14 - Angular migration from v12 to v14 Safari 中带有 Angular (v14) 应用程序的空白页面 - Blank page with Angular (v14) app in Safari 将 angular 更新到 v14 后,TSlint 规则变为 ESlint - TSlint rules to ESlint after update angular to v14 如何在 Angular(v14) 中更新@ViewChildren(或@ContentChildren)的 styles - How to update the styles of @ViewChildren (Or @ContentChildren) in Angular(v14) Angular 使用@angular-builders/custom-webpack 将 v13 迁移到 v14 - Angular migration v13 to v14 using @angular-builders/custom-webpack 找不到模块“@angular/cdk/dialog”- Angular 将 v13 升级到 v14 问题 - Cannot find module '@angular/cdk/dialog' - Angular upgrade v13 to v14 issue Angular Material v15 主题如何从 v14 工作 - How dose Angular Material v15 theming work coming from v14 如何在 angular v14 中的纯 function 中使用 class 外部注入? - How to use inject outside class in pure function in angular v14? ProvidedIn:Angular 14+ 中的“any”已弃用。 什么是好的替代品? - ProvidedIn: 'any' in Angular 14+ is deprecated. What would be a good replacement? ng 测试在 v14 中不再通过 Jest CLI 选项? - ng test not passing Jest CLI options any more in v14?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM