如何在外部配置文件中定义变量并在app.module中使用它

时间:2017-12-09 23:09:19

标签: angular typescript ionic3 angular5 angular-aot

在带有Angular 5的Ionic 3中,我试图在app.config.ts文件中定义所有外部API密钥。

我必须声明一个类,否则我会收到ngc引发的AOT错误。

这是我的app.config.ts

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

@Injectable()
export class AppConfig {
  public ONESIGNAL_API_KEY: string;
  public FIREBASE_CONFIG: any;
  public MIXPANEL_TOKEN: string;

  constructor() {
    this.ONESIGNAL_API_KEY = 'myoskey';
    this.FIREBASE_CONFIG = {
      apiKey: "myfbkey",
      authDomain: "myfbdomain
      };
    this.MIXPANEL_TOKEN = 'mymptoken';
  }
}

我可以在其他类的构造函数中注入它,但firebase必须在app.module.ts中初始化。所以我试着做以下事情 //配置

import {AppConfig} from './app-config';
...
imports: [
    BrowserModule,
    HttpClientModule,
    IonicModule.forRoot(MyApp, {
      backButtonText: '',
      tabsHideOnSubPages: true,
      //scrollAssist: true,
      //autoFocusAssist: true
    }),
    IonicStorageModule.forRoot(),
    IonicImageLoader.forRoot(),
    AngularFireModule.initializeApp(AppConfig.FIREBASE_CONFIG),
    ....

不幸的是我收到以下错误 Property 'FIREBASE_CONFIG' does not exist on type 'typeof AppConfig'.

如何在此FIREBASE_CONFIG文件中声明app.config并直接在app.module中使用它?

我试图在构造函数之前直接初始化变量,但结果相同

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

@Injectable()
export class AppConfig {
  public ONESIGNAL_API_KEY: string;
  public FIREBASE_CONFIG: any = {
      apiKey: "myfbkey",
      authDomain: "myfbdomain
      };
  public MIXPANEL_TOKEN: string;

  constructor() {
    this.ONESIGNAL_API_KEY = 'myoskey';
    this.MIXPANEL_TOKEN = 'mymptoken';
  }
}

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

你可以使用枚举:

import {AppConfig ,FIREBASE_CONFIG  } from './config';
...
imports: [
    //...
    AngularFireModule.initializeApp(FIREBASE_CONFIG)

你就像你一样使用它:

AppConfig.FIREBASE_CONFIG

注意:您收到该错误是因为您尝试访问FIREBASE_CONFIGAppConfig Post.objects.extra(select={'last_update': 'select max(c.create_date) from comment_comment c where c.post_id = post_post.id'}).order_by('-last_update') 类的非静态属性。