【发布时间】:2018-04-06 06:06:19
【问题描述】:
我希望在启动时加载数据,例如使用 LocationsService 的国家/地区。
我实现了当前的服务:
...
@Injectable()
export class LocationsService {
public countries: Country[];
constructor(private http: HttpClient) {
}
public getCountries() {
if (this.countries.length == 0) {
this.http.get<Country[]>(`${this.url}`)
.subscribe(data => { this.countries = data; },
err => console.error(err),
() => console.log(this.countries)
)
};
return this.countries;
}
}
我已尝试将服务放入引导程序中:
bootstrap: [AppComponent, LocationsService]
但它不起作用(实际上引发了错误)。我需要这种类型的列表从启动时可用(仅加载 1 次)。谢谢!
【问题讨论】:
-
如果你想加载一次然后在appModule中使用这个服务
-
将服务添加到:提供者:[ LocationsService ]
标签: angular