【问题标题】:Unsafe member access .setAttribute on an `any` value. eslint(@typescript-eslint/no-unsafe-member-access)对“任何”值的不安全成员访问 .setAttribute。 eslint(@typescript-eslint/no-unsafe-member-access)
【发布时间】: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


    【解决方案1】:

    解决方案 -

    const element: HTMLElement = this.elementRef.nativeElement as HTMLElement;
    element.setAttribute('key', keyword);
    

    【讨论】:

      猜你喜欢
      • 2021-09-29
      • 2021-08-10
      • 1970-01-01
      • 2021-06-02
      • 2020-11-04
      • 1970-01-01
      • 2020-10-01
      • 1970-01-01
      • 2023-02-26
      相关资源
      最近更新 更多