我正在做我的第一个有角项目,而我的home组件html如下所示
<div class="container">
<div>
<div class="col-md-4">
<h1 id="tableLabel">Latest</h1>
<news-item [section]="Latest"></news-item>
</div>
<div class="col-md-4">
<h1 id="tableLabel">STEM Stuff</h1>
<news-item [section]="STEM"></news-item>
</div>
</div>
</div>
和新闻项目的ts如下
import { Component, Inject, Input } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'news-item',
templateUrl: './news-item.component.html',
})
export class NewsItemComponent {
public newsitems: NewsItem[];
@Input() section: string;
constructor(http: HttpClient, @Inject('BASE_URL') baseUrl: string) {
http.get<NewsItem[]>(baseUrl + 'newsitems/GetNewsItemsBySection/' + this.section).subscribe(result => {
this.newsitems = result;
}, error => console.error(error));
}
}
我得到this.section
的undefined
构造函数中。我想念什么?
尝试在ngOnInit()
或ngOnChanges()
和构造函数之间的区别ngOnInit
是ngOnInit
生命周期挂钩在构造函数之后运行。输入初始值在构造函数中不可用,但在ngOnInit
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句