【发布时间】:2019-02-14 06:30:58
【问题描述】:
好的,伙计们,我是 Angular 的新手,我遇到了一个问题,我不知道我做错了什么。
我的父组件看起来像这样,我正在尝试将 weekly 变量传递给我的子组件:
app.component.ts
import { Component } from "@angular/core";
import { GeolocationService } from "./geolocation.service";
import { WeatherService } from "./weather.service";
import { kmphToMs } from '../utilities/helpful';
@Component({
selector: "app-root",
templateUrl: "./app.component.html",
styleUrls: ["./app.component.css"]
})
export class AppComponent {
latitude: number;
longitude: number;
cityName: string;
currentTemp: number;
currentHumidity: number;
currentWindSpeed: string;
weekly: Array<object>;
erroMessage: string;
constructor(
private geolocationService: GeolocationService,
private weatherService: WeatherService
) {}
ngOnInit() {
this.geolocationService.getCoordinates().subscribe(result => {
console.log(result);
this.latitude = result.coords.latitude;
this.longitude = result.coords.longitude;
this.weatherService
.getTheWeather(this.latitude, this.longitude)
.subscribe(weatherData => {
console.log(weatherData);
this.cityName = weatherData["timezone"];
this.currentTemp = weatherData["currently"]["temperature"];
this.currentWindSpeed = kmphToMs(weatherData["currently"]["windSpeed"]);
this.currentHumidity = weatherData['currently']['humidity'] * 100;
this.weekly = weatherData['daily']['data'];
console.table(this.weekly);
});
});
}
}
app.component.html
<app-days
[weekly]="weekly"
></app-days>
这就是我的子组件的样子:
import { Component, OnInit, Input } from "@angular/core";
@Component({
selector: "app-days",
templateUrl: "./days.component.html",
styleUrls: ["./days.component.css"]
})
export class DaysComponent implements OnInit {
@Input() weekly: Array<object>;
constructor() {
}
ngOnInit() {
console.log(this.weekly);
}
}
我正在尝试 console.log 每周变量,但它说它未定义,我不知道为什么
【问题讨论】:
标签: angular typescript