【发布时间】:2023-02-18 18:55:05
【问题描述】:
以下代码抛出错误,但代码仍按预期运行。
import { ..., ElementRef } from '@angular/core';
constructor(
...,
private elementRef: ElementRef
) { }
ngOnInit(): void {
const keyword: string = 'xyz';
this.elementRef.nativeElement.setAttribute('key', keyword); // gives error
}
错误 -
不安全成员访问“任何”值的 .setAttribute。 eslint(@typescript-eslint/no-unsafe-member-access)
解决办法是什么?
我试图将“关键字设置为字符串”,但没有用。
this.elementRef.nativeElement.setAttribute('key', keyword as string);
【问题讨论】:
标签: angular typescript eslint typescript-eslint