我有json:
public example=[{
array:[item1, item2,]
},
{
array:[item1, item2,]
}]
html文件:
<div *ngFor="let item of example">
<div *ngIf="check(item.array)"> ...</div>
</div>
但是在ngOnInit()中调用函数检查,如果出错
check(...request: string[]){
return this._RolesService.checkRoles(request);
}
ngOnInit(): void {
let req:string[] = ['string1'];
if(this.check(req)){}
}
gender html是ok的,所以当我在ngOnInit中调用函数check()时,它有错误:类型'string[]'的参数不能分配给类型'string'的参数。
你应该把你的代码改成:
let req:string[] = ['string1'];
if(this.check(...req)){}
或
let req = 'string1';
if(this.check(req)){}
可以分享一下this._RolesService.checkRoles(request)
的代码吗?我认为这个函数接受string
作为参数,但你传递的是['string1']
,这是一个字符串数组。这就是抛出错误的原因。
根据你目前提供的信息,我认为这就是问题所在。
纠正解决方案:由于此解决方案下的注释,解决问题的方法之一是在调用
方法时使用扩展操作符:this.check(...req)