微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

angular – 当ng build –prod(AOT)时,装饰器不支持函数调用

问题类型:错误/问题

描述

我正在使用ng-packagr lib将我的库编译为js.我编译了所有内容没有任何问题,但是当我想要使用ng build –prod(启用AOT)来使用我的库时,我收到错误

在“AppModule”的模板编译期间出现错误错误在装饰器中不支持函数调用,但调用了“BsDropdownModule”.

当我删除.forRoot方法时,我收到错误

ERROR in:由模块导入的/home/sf/Desktop/Developerka/kompilacja/final/sample-repo/node_modules/angular-library-name/free/dropdown/dropdown.module.d.ts’中的意外值’BsDropdownModule’ AppModule位于/home/sf/Desktop/Developerka/kompilacja/final/sample-repo/src/app/app.module.ts’.请添加@NgModule注释

请注意,ng –prod –aot = false
没有产生任何错误.

如何重现:

下载repo:https://github.com/Bloodcast69/aot-error,输入
npm安装
ng build –prod.

预期的行为

想用AOT构建没有错误(我需要它与Angular Universal兼容)
版本信息

ng-packagr:2.4.1
@ angular / *:5.2.9
打字稿:2.5.3
rxjs:5.5.6
节点:8.1.0
npm / yarn:npm:5.6.0

文件

app.module.ts:

import { BsDropdownModule } from 'angular-library-name';
import { browserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';



import { AppComponent } from './app.component';


@NgModule({
  declarations: [
    AppComponent
  ],imports: [
    BsDropdownModule.forRoot(),browserModule
  ],providers: [],bootstrap: [AppComponent]
})
export class AppModule { }

dropdown.module.d.ts:

import { ModuleWithProviders } from '@angular/core';
export declare class BsDropdownModule {
    static forRoot(config?: any): ModuleWithProviders;
}

dropdown.module.ts(在编译成JS之前):

import { ModuleWithProviders,NgModule } from '@angular/core';
import { ComponentLoaderFactory } from '../utils/component-loader/index';

import { PositioningService } from '../utils/positioning/index';
import { BsDropdownContainerComponent } from './dropdown-container.component';
import { BsDropdownMenuDirective } from './dropdown-menu.directive';
import { BsDropdownToggleDirective } from './dropdown-toggle.directive';
import { BsDropdownConfig } from './dropdown.config';

import { BsDropdownDirective } from './dropdown.directive';
import { BsDropdownState } from './dropdown.state';

@NgModule({
  declarations: [
  BsDropdownMenuDirective,BsDropdownToggleDirective,BsDropdownContainerComponent,BsDropdownDirective
  ],exports: [
  BsDropdownMenuDirective,entryComponents: [BsDropdownContainerComponent]
})
export class BsDropdownModule {
  public static forRoot(config?: any): ModuleWithProviders {
    return {
      ngModule: BsDropdownModule,providers: [
      ComponentLoaderFactory,PositioningService,BsDropdownState,{provide: BsDropdownConfig,useValue: config ? config : {autoClose: true}}
      ]
    };
  };
}

注意
我已经阅读了整个互联网,找到了对我有帮助的东西,但没有任何成功.我检查了这个主题

FeatureModule fails during an AOT build when static forRoot has arguments

https://github.com/angular/angular/issues/14707

如果缺少一些必要的信息,请告诉我,我会提供.

谢谢,
Bloodcast69

解决方法

这是AOT的一个问题:forRoot函数需要在编译时执行.在更新版本的Angular上它应该按原样运行,否则你可能会运气玩tsconfig.app.json.检查此相关问题: Angular 6 Prod Function calls are not supported in decorators but ‘..Module’ was called

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐