Angular 7-未捕获(承诺):错误:StaticInjectorError(AppModule)[BASE_URL]

时间:2018-12-02 19:05:40

标签: angular typescript

我的组件类如下:

  export class FetchDataComponent {
  public forecasts: WeatherForecast[];

  constructor(http: HttpClient, @Inject('BASE_URL') baseUrl: string) {
    http.get<WeatherForecast[]>(baseUrl + 'api/SampleData/WeatherForecasts').subscribe(result => {
      this.forecasts = result;
    }, error => console.error(error));
  }
}

将项目从Angular 5更新为7后,我开始收到以下控制台错误:

  

未捕获(承诺):错误:StaticInjectorError(AppModule)[BASE_URL]

因此,我尝试通过在environment.ts中定义BASE_URL,然后将其导入到app.module.ts中,并将其添加到其providers数组中来解决上述错误。

现在我的新环境如下:

export const environment = {
  production: false,
  BASE_URL: 'https://localhost:4430'
};

和我的app.module.ts如下:

import { HttpClientModule } from '@angular/common/http';
import { RouterModule } from '@angular/router';
import { AppComponent } from './app.component';
import { FetchDataComponent } from './fetch-data/fetch-data.component';


@NgModule({
   declarations: [
      AppComponent,
      FetchDataComponent
   ],
   imports: [
      BrowserModule.withServerTransition({ appId: 'ng-cli-universal' }),
      HttpClientModule,
      BASE_URL
    ])
  ],
  providers: [BASE_URL],
  bootstrap: [AppComponent]
})

但是现在无法识别BASE_URL,并且出现编译错误:

  

找不到名称“ BASE_URL”

我在这里做什么错了?

0 个答案:

没有答案