【发布时间】:2019-04-17 13:23:11
【问题描述】:
我不明白为什么这项服务不起作用?是不是依赖有问题?
服务:
import { Http } from '@angular/http';
export class GetWeatherService {
constructor(private http: Http) {}
getWeather() {
return this.http.get(link);
}
}
和组件:
import { Component, OnInit } from '@angular/core';
import { GetWeatherService } from './get-weather.service';
@Component({
selector: 'app-form',
templateUrl: './form.component.html',
styleUrls: ['./form.component.css'],
providers: [GetWeatherService]
})
export class FormComponent implements OnInit {
constructor(public getWeather: GetWeatherService) {}
ngOnInit() {
this.getWeather.getWeather();
}
}
【问题讨论】:
-
hagner 的答案是您所需要的,但我也可以建议使用新的 HttpClient 而不是“Http”。它有许多改进:-)
标签: angular typescript