建立aot时共享角度2模块抛出错误

时间:2017-05-15 17:29:23

标签: angular angular2-services angular2-directives

我创建了一个共享的角度2模块,用于分离可重用的组件。一切正常但在构建aot时遇到以下错误,pfb错误

Error encountered resolving symbol values statically. Calling function 'makeDecorator', function calls are not supported. Consider replacing the function or lambda with a reference to an exported function, resolving symbol Injectable in D:/Shared_Module/node_modules/@angular/core/src/di/metadata.d.ts, resolving symbol OpaqueToken in D:/ Shared_ Module/node_modules/@angular/core/src/di/opaque_token.d.ts, resolving symbol OpaqueToken in D:/Shared_ Module/node_modules/@angular/core/src/di/opaque_token.d.ts

PFB示例代码和代码结构

Base_Module
------------------
|
|___module.ts @NgModule
|
|___components
|
|___node_modules
|
|___package.json
|
|___tsconfig.json

Shared_Module
------------------
|
|___module.ts @NgModule
|
|___components -> Shared_Components
|
|___node_modules
|
|___package.json
|
|___tsconfig.json

以下是Base模块中的module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { Shared_Module } from './../../../../SharedModule/module'; --**Including the shared module here**

@NgModule({
  declarations: [
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    SharedModule  -- **New shared module**
  ],
  exports:   [],
  bootstrap: []
})

export class Base_Module { }

以下是共享模块中的module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';

@NgModule({
  declarations: [
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule
  ],
  exports:   [Shared_Component],
  bootstrap: []
})

export class Shared_Module { }

这是tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "module": "es2015",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": ["es2015", "dom"],
    "noImplicitAny": true,
    "suppressImplicitAnyIndexErrors": true
  },

  "files": [
    "src/app/module.ts",
    "src/main.ts"
  ],

  "angularCompilerOptions": {
   "genDir": "aot",
   "skipMetadataEmit" : true
}
}

应用程序运行正常。 aot编译可能有什么问题。欢迎所有帮助。提前谢谢!

1 个答案:

答案 0 :(得分:0)

首先,应该导入CommonModule,而不是BrowserModule(在共享模块中)。

见这里:

https://angular.io/docs/ts/latest/cookbook/ngmodule-faq.html#!#q-browser-vs-common-module

其次,OpaqueToken已被弃用:

https://angular.io/docs/ts/latest/api/core/index/OpaqueToken-class.html

改为使用InjectionToken

https://angular.io/docs/ts/latest/api/core/index/InjectionToken-class.html

看看这些事情是否有帮助。