我想首先说我对 angular 和 typescript 都很陌生。
我用 Angular 编写了一个程序,我使用路由在另一个页面上显示更多信息。
错误发生在 ngOnInit(){
第一个错误发生在:this.blomId TS2322: Type 'string | null '不可分配给类型'数字'。“null”类型不能分配给“number”类型。
第二个错误发生在:data[this.blomId] TS7053:元素隐式具有“任何”类型,因为“数字”类型的表达式不能用于索引“对象”类型。在“对象”类型上找不到具有“数字”类型参数的索引签名。
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { BlommorService } from '../blommor.service';
@Component({
selector: 'app-info',
templateUrl: './info.component.html',
styleUrls: ['./info.component.scss']
})
export class InfoComponent implements OnInit {
blomId: number;
valdblomma: any;
constructor(private activatedRoute: ActivatedRoute, private blomservice: BlommorService) {this.blomId = 0;}
ngOnInit() {
this.blomId =this.activatedRoute.snapshot.paramMap.get('id');
this.blomservice.getBlommor().subscribe((data) => {
this.valdblomma = data[this.blomId];
});
}
}
感谢帮助。