InjectionToken产生“在遇到静态解析符号值时遇到错误”

时间:2017-12-09 15:19:54

标签: angular typescript ionic-framework ionic2 ionic3

我正在尝试使用ionic cordova build ios --prod

构建一个Ionic 3应用

一切都工作得很好但是突然(在我更新包装更新后)我不能再建造了。

我认为错误是由app.config.ts中的InjectionToken引起的,我根据Angular 4 docs编码了

app.config.ts

import { InjectionToken } from '@angular/core';

export interface OneSignalConfig {
  apiKey: string
};

// PROD Config

export const ONESIGNAL_CONFIG: OneSignalConfig = {
  apiKey: '**apiKey**'
};

export const firebaseConfig = {
  ...
};

export const mixpanelToken = "**mytoken**";

// COMMON
export let ONESIGNAL_CONFIG_TOKEN = new InjectionToken<OneSignalConfig>('onesignal.config');

app.module.ts

// Config imports
import { firebaseConfig, ONESIGNAL_CONFIG, ONESIGNAL_CONFIG_TOKEN } from './app-config.ts';
...
// the providers part
providers: [
    ...
    OneSignal,
    { provide: ONESIGNAL_CONFIG_TOKEN, useValue: ONESIGNAL_CONFIG },
...
  ]

在尝试构建时会产生以下问题:

[16:10:19]  ngc started ...
[16:10:27]  typescript error
            Error encountered resolving symbol values statically. Could not resolve ./app-config.ts relative to
            /Users/me/myproj/src/app/app.module.ts., resolving symbol
            AppModule in /Users/me/myproj/src/app/app.module.ts,
            resolving symbol AppModule in
            /Users/me/myproj/src/app/app.module.ts, resolving symbol
            AppModule in /Users/me/myproj/src/app/app.module.ts

Error: The Angular AoT build failed. See the issues above
    at /Users/me/myproj/node_modules/@ionic/app-scripts/dist/aot/aot-compiler.js:237:55
    at step (/Users/me/myproj/node_modules/@ionic/app-scripts/dist/aot/aot-compiler.js:32:23)
    at Object.next (/Users/me/myproj/node_modules/@ionic/app-scripts/dist/aot/aot-compiler.js:13:53)
    at fulfilled (/Users/me/myproj/node_modules/@ionic/app-scripts/dist/aot/aot-compiler.js:4:58)
[16:10:27]  copy finished in 7.84 s

但它适用于serverunemulate ......这让我发疯了

2 个答案:

答案 0 :(得分:1)

尝试将其简化为以下形式:

import { InjectionToken } from "@angular/core";

    export let APP_CONFIG= new InjectionToken<AppConfig >('app.config');

    export interface AppConfig {
      apiKey: string
      // Any other constants you want here.
    }

    export const AppConstants: AppConfig = {
      apiKey: '**apiKey**'
      // Any other values you want here matching above interface.
    }

然后在您的模块中:

// Config imports
import { firebaseConfig, ONESIGNAL_CONFIG, ONESIGNAL_CONFIG_TOKEN } from './app-config.ts';
...
// the providers part
providers: [
    ...
    OneSignal,
    { provide: APP_CONFIG, useValue: AppConstants },
...
  ]

然后在您的组件中:

...

import { Inject } from '@angular/core';
import { APP_CONFIG, AppConfig } from '../../app/app.config';

...

constructor(@Inject(APP_CONFIG)
    private config: AppConfig,
) {
  console.log(this.config.apiKey) // do something with your constants.
}

答案 1 :(得分:0)

您的导入语句中不应包含文件扩展名。从app.module.ts中删除.ts。这应该起作用。

import { firebaseConfig, ONESIGNAL_CONFIG, ONESIGNAL_CONFIG_TOKEN } from './app-config';

我在ts处收到了另一条错误消息,因此您必须使用旧的角度版本。