angular - 在 Angular 2 应用程序中使用接口(interface)和 OpaqueToken 时出现 Typescript 警告

标签 angular typescript

我一直在关注文档 here并使用 ng-cli

我创建了以下配置文件 (app-config.ts):

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

export interface AppConfig {
  supportTelephoneNumber: string;
}

export let APP_CONFIG_t = new OpaqueToken('app.config');

export const APP_CONFIG: AppConfig = {
  supportTelephoneNumber: '1111 111 1111'
};

在我的 app.module.ts 文件中我有:

...
@NgModule({
  declarations: [
    UkCurrencyPipe,
    AppComponent,
    HomeComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    RouterModule.forRoot(ROUTES, { useHash: true }),
    MaterialModule.forRoot()
  ],
  providers: [
    { provide: APP_CONFIG_t, useValue: APP_CONFIG },
    ...

我在我的 app.component.ts 文件中使用此配置,如下所示:

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

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.less']
})
export class AppComponent {
  constructor(@Inject(APP_CONFIG_t) public config: AppConfig) {

  callSupport(): void {
    window.location.href = 'tel:+' + this.config.supportTelephoneNumber;
  }
}

当我使用 ng serve 为我的应用提供服务时,一切似乎都运行良好,但我确实在运行 ng server 的控制台中看到了这些警告:

WARNING in ./src/app/app.component.ts
40:166 export 'AppConfig' was not found in './app-config'

WARNING in ./src/app/app.component.ts
40:195 export 'AppConfig' was not found in './app-config'

有谁知道这些警告的含义以及我是否应该担心它们?

我的版本

  • 操作系统:Mac OS X El Capitan v10.11.6
  • ng-cli: v1.0.0-beta.16
  • Angular :v2.0.1
  • typescript :v2.0.2

最佳答案

根据 comment关于问题https://github.com/angular/angular-cli/issues/2034

having the same issue. (Works fine despite warning) are you exporting more than one interface/class/const from the file? issue stopped for me after i exported each interface from its own dedicated file.

meaning if i had one file with multiple exports - i got warnings in build (export 'MyInterface1' was not found in '../file')

file.ts

export interface MyInterface1 {}
export interface MyInterface2 {}

after refactor - no warning

file1.ts

export interface MyInterface1 {}

file2.ts

export interface MyInterface2 {}

关于angular - 在 Angular 2 应用程序中使用接口(interface)和 OpaqueToken 时出现 Typescript 警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39857565/

相关文章:

Angular 5 : Module not found: Error: Can't resolve '@angular/forms/src/validators'

javascript - Angular 2 与 TypeScript : View not updating after setting variable in response handler

javascript - 错误 TS2339 : Property 'log' does not exist on type '{ new (): Console; prototype: Console; }'

javascript - 使用外部 JS 库的 Angular 2 组件

angular - ngTemplateOutletContext 上下文源未定义

Angular 7 未发送 xhr 请求

angular - 如何在库中声明/包含导入的 scss 文件夹,以便可以在应用程序的 scss 文件中调用 @import?

javascript - 在 Typescript 中使用多个字符串数组

typescript - 使用严格的 typescript 创建商店

compiler-construction - 在 typescript 中是否可以让函数返回扩展它的类的类型