angular - 属性 'brews' 没有初始化程序,也没有在构造函数中明确分配
问题描述
当我尝试运行我的项目时出现此错误,我尝试在 angularCompilerOptions 中设置 strictPropertyInitialization false 但它也不起作用我尝试设置!而是酿造
import { HttpService } from '../http.service';
@Component({
selector: 'app-list',
templateUrl: './list.component.html',
styleUrls: ['./list.component.scss']
})
export class ListComponent implements OnInit {
brews: Object;
constructor(private _http: HttpService) { }
ngOnInit() {
this._http.getBeer().subscribe(data => {
this.brews = data
console.log(this.brews);
});
}
}
<h1>Breweries</h1>
<ul *ngIf="brews">
<li *ngFor="let brew of brews">
<p class="name">{{ brew.name }}</p>
<p class="country">{{ brew.country }}</p>
<a class="site" href="{{ brew.website_url }}">site</a>
</li>
</ul>
解决方案
我们也面临同样的错误。可能是 Angular 的不同版本造成的。
将 ListComponent 类更改为以下内容:
brews: Array<any> = [];
constructor( private _http:HttpService) { }
ngOnInit(): void {
this._http.getBeer().subscribe(data => {
this.brews = data as Array<Object>;
console.log(this.brews);
});
}
这个答案归功于 ram64 在 youtube 上的评论。
推荐阅读
- python - PANDAS 从列中找到确切的给定字符串/单词
- django - 在 DetailView 的上下文中重复的对象
- java - 颜色取决于百分比
- amazon-web-services - 具有 IP 限制的 AWS WAF
- javascript - Next 和 Prev 按钮不适用于多个日历
- java - 无法处理配置类的导入候选者错误即将到来
- python - Flask - 如何存储可在所有蓝图中访问的值
- swift - 将 Swift Vapor 项目升级到 Swift 4 / Catalina 会导致大量未声明的类型
- python - 在 python 中运行函数之前,itertools.product() 的 len()
- platypus - Platypus NSGA-II 显示 '' unhashable type: 'numpy.ndarray' '',经过 200 次评估