angular - InjectionToken 产生 "Error encountered resolving symbol values statically"

标签 angular typescript ionic-framework ionic2 ionic3

我正在尝试使用 ionic cordova build ios --prod 构建 Ionic 3 应用程序

一切正常,但突然(我猜是在更新包之后)我无法再构建了。

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

应用程序配置.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');

应用程序模块.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 一起使用...这让我抓狂

最佳答案

试着把它简化成这样:

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.
}

关于angular - InjectionToken 产生 "Error encountered resolving symbol values statically",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47730038/

相关文章:

node.js - 如何在我的 IDE 中添加 Intellisense

typescript - Electron 和 TypeScript( Electron 生成器): "Cannot use import statement outside a module"

angularjs - Angular2 和 TSD 出现 "Duplicate identifier"错误

angularjs - window.cordova 在 ionicPlatform.ready 上未定义

javascript - Ionic 框架中的 Cordova 插件媒体兼容性问题

ios - ionic 构建 ios **导出失败** 错误 : exportArchive: Found no compatible export methds

angular - 拖动以在页面周围移动组件?

css - 悬停效果上的 Angular Kendo Grid Row

ngFor 循环中点击的 div 的 Angular Return id

javascript - Nest.JS 和 Observables