官术网_书友最值得收藏!

Providers

You'll note that the number of errors didn't go down. Instead, AppComponent and CurrentWeatherComponent are failing to be created due to a missing provider for WeatherService. So, let's add the provider for WeatherService to the spec files for both components.

  1. Provide the WeatherService in the declarations in app.component.spec.ts
  2. Apply the same code change in current-weather.component.spec.ts, as shown:
src/app/app.component.spec.ts
src/app/current-weather/current-weather.component.spec.ts
...
beforeEach(
async(() => {
TestBed.configureTestingModule({
declarations: [...],
providers: [WeatherService],
...

You may wonder why AppComponent is needing a provider, since the component constructor is not injected with the WeatherService. This is happening because CurrentWeatherComponent is a hard-coded dependency of AppComponent. It is possible to decouple the two components further in two ways: one way is to inject the component dynamically using an ng-container, and the other would be to leverage Angular Router and router-outlet. The latter option is how you will be structuring the vast majority of your applications and will be covered in the later chapters, and implementing the former option to properly decouple the components is left as an exercise for the reader.

主站蜘蛛池模板: 澄城县| 精河县| 宁城县| 鹤庆县| 富裕县| 道真| 宁津县| 大宁县| 寿宁县| 吕梁市| 修水县| 西峡县| 南木林县| 安陆市| 霍山县| 泰兴市| 肃宁县| 枞阳县| 慈溪市| 上思县| 阜城县| 潍坊市| 台南县| 波密县| 宝兴县| 石狮市| 新巴尔虎右旗| 疏附县| 平和县| 汾阳市| 宕昌县| 修文县| 武冈市| 横山县| 招远市| 民县| 瑞昌市| 华宁县| 苏尼特右旗| 北安市| 灵台县|