-1
我正在开发与Angular的天气应用。我是Angular的新手。我想带来我选择的城市的天气信息。但我无法将数据发送到第二页。哪里有问题?预先感谢您的帮助。Angular 2发送阵列另一页
export class ForecastComponent implements OnInit, OnDestroy {
constructor(private service: WeatherService, private router: Router, private route: ActivatedRoute) { }
public items: Array<string> = ["ADANA", "ADIYAMAN", "AFYONKARAHİSAR", "AĞRI", "AMASYA", "ANKARA", "ANTALYA", "ARTVİN"];
public selectedValue: BaseModel;
value: any = {};
weatherClass: Weather;
ngOnInit() {}
ngOnDestroy(): void {
this.route.data.subscribe(
(data: { weatherClass: Weather }) => {
this.weatherClass = data.weatherClass;
}
)
}
public selected(value: any): void {
console.log('Selected value is: ', value);
}
public removed(value: any): void {
console.log('Removed value is: ', value);
}
public typed(value: any): void {
console.log('New search input: ', value);
}
public refreshValue(value: any): void {
this.value = value;
}
sendCityWeather(value: Array<BaseModel>) {
this.service.otherWeather(this.value.text).subscribe(
(data) => {
this.weatherClass = new Weather(data.name, data.main.temp, data.weather[0].description, data.main.temp_min, data.main.temp_max, data.weather[0].icon);
console.log(this.weatherClass);
this.router.navigate(['/weather']);
}
)
}
}
export class WeatherComponent implements OnInit, OnDestroy {
weatherClass: Weather;
value: any = {};
constructor(private service: WeatherService, private route: ActivatedRoute, private router: Router) {}
ngOnInit() {
this.service.otherWeather(this.value.text).subscribe(
(data: Weather) => {
this.weatherClass = data;
}
)
}
ngOnDestroy(): void {
}
export class WeatherService {
weatherClass: Weather;
constructor(private http:Http) { }
otherWeather(city:string){
return this.http.get(`http://api.openweathermap.org/data/2.5/weather?appid=0f3fb9fa31ad3d41f1bb2bd0841c3f2f&q=${city}&units=imperial&cnt=10`).map((response:Response) => response.json());
}
}
谢谢你的回答。但我想要发送的数据是从otherWeather()服务获取所选城市信息的数据。 –
我已经更新了Service,Parent和Child组件。这有帮助吗? –
是的。解决了。非常感谢。 –