在将数据分配给我的角度组件之前尝试过滤数组时遇到了一些问题,我有一个提示数组,但我最初只想显示 1 个提示
我的问题是,当我尝试通过匹配 ID 1 的提示过滤我的数组时,我不断收到
"'hint' 隐式具有 'any' 类型。"在 cli 中,但这应该没问题,因为它只是一个计数器
我在我的 tsconfig.json 中禁用了字符串注入参数,并遵循了其他各种关于禁用严格的打字稿注入规则的帖子,但我无法停止向我询问计数器的类型,因此我无法使用过滤器功能这里
import { Component,Input,OnInit } from '@angular/core';
import { SpecsService } from 'src/app/specs.service';
import {Hint} from './hint';
@Component({
selector: 'app-hintbox',templateUrl: './hintbox.component.html',styleUrls: ['./hintbox.component.css']
})
export class HintboxComponent implements OnInit {
currentHint: Hint[]=[]
hint: Hint={hint:'',id:0}
constructor(private specsService: SpecsService) { }
ngOnInit(): void {
this.specsService.getHints().subscribe((response)=> this.currentHint= response.filter(hint:Hint => hint.id===1));
}
}
希望得到任何建议,或者如果以后有更合理的方法来过滤数据那将是惊人的
这是我的规范服务功能,它指向我的虚拟 json 服务器
export class SpecsService {
private apiUrl = 'http://localhost:3000'
constructor(private http:HttpClient) {}
getHints(): Observable<any>{
return this.http.get(`${this.apiUrl}/Hints`)
}
}