首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用ɵ编译组件(Angular 9)创建的带有动态模板的Angular组件不能在生产模式下工作

使用ɵ编译组件(Angular 9)创建的带有动态模板的Angular组件不能在生产模式下工作
EN

Stack Overflow用户
提问于 2021-05-27 10:35:05
回答 1查看 49关注 0票数 0

我在Angular 9中创建了一个动态组件。我在html模板中有一个<ul>标记,它的子元素是从服务器动态加载的(服务器将使用handlebar模板返回像<li>One</li><li>Two</li><li (click)="onLinkClicked(3)">Three</li>这样的值)。

代码语言:javascript
复制
    private createComponentFromRaw(template: string, containerRef: ElementRef) {
        class DynamicComponent {
            onLinkClicked(resource: any) {
                console.log(resource);
            }
        }

        ɵcompileComponent(DynamicComponent, { template, changeDetection: ChangeDetectionStrategy.OnPush });
        ɵrenderComponent(DynamicComponent, {
            host: containerRef.nativeElement,
            injector: this.injector,
            hostFeatures: [ɵLifecycleHooksFeature],
        });
    }

在调用this.createComponentFromRaw('<li>One</li>', this.ref.element);时,组件在以ng serve身份运行时会按预期呈现,但在生产模式(ng build --prod)下运行时会抛出以下错误:

代码语言:javascript
复制
ERROR Error: Angular JIT compilation failed: '@angular/compiler' not loaded!
  - JIT compilation is discouraged for production use-cases! Consider AOT mode instead.
  - Did you bootstrap using '@angular/platform-browser-dynamic' or '@angular/platform-server'?
  - Alternatively provide the compiler with 'import "@angular/compiler";' before bootstrapping.
EN

回答 1

Stack Overflow用户

发布于 2021-05-27 20:28:57

看起来目前唯一可用的选项是在angular.json中设置buildOptimizer: false

这是因为buildOptimizer错误地认为@angular/编译器没有副作用,并将其作为树摇动的一部分删除

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67714946

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档