【问题标题】:Angular error- "The left-hand side of an assignment expression may not be an optional property access.ts"?角度错误-“赋值表达式的左侧可能不是可选属性 access.ts”?
【发布时间】:2021-05-07 06:54:53
【问题描述】:

在我的 Angular 打字稿文件中,我有以下代码。我需要帮助来解决这个打字稿错误

ngAfterViewInit() {
 setTimeout(() => {
 this.tada = document.querySelectorAll('.highlighted').length;
document.querySelector<HTMLElement>('.highlighted')?.style.backgroundColor = 'pink';->>Error
        this.fckme();
      }, 50);
    }

我收到以下错误

The left-hand side of an assignment expression may not be an optional property access.ts(2779)

我创建了自定义管道,如果我在文本中循环并搜索单词,如果给定文本中存在单词,我会向它添加突出显示的类,以便我可以用粉红色突出显示该单词

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'highlight'
})
export class HiPipe implements PipeTransform {

  transform(v1: string, v2: string): unknown {
   //some code
    for (const match of matches) {
      value = value.replaceAll(match, `<span class = "highlighted data-${match}">${match}</span>`);
    }
    return v1;
  }
}

【问题讨论】:

    标签: angular typescript runtime-error


    【解决方案1】:

    试试这个:

    这可能有助于其他人延迟回复。

    document.querySelector<HTMLElement>('zsiq_floatmain zsiq_theme1 siq_bL')[0] ['style'].display = 'none'
    

    【讨论】:

      【解决方案2】:

      把里面的问号去掉就行了

      document.querySelector<HTMLElement>('.highlighted')?.style.backgroundColor = 'pink';
      

      对于错误。

      应该是

      if(document.querySelector<HTMLElement>('.highlighted')){
          document.querySelector<HTMLElement>('.highlighted').style.backgroundColor = 'pink';
      }
      

      因为那?将 document.querySelector('.highlighted') 设为可选,在右侧我们为其赋值。

      但它会在第一个具有“突出显示”类的项目中设置粉红色。所以你需要循环遍历元素或者给它添加一个粉色类。并将该类项的背景颜色设置为粉红色

      尝试使用以下代码更改突出显示类的所有元素。

      const elements = document.querySelectorAll('.highlighted');
          if(elements.length){
            elements.forEach((item:HTMLElement) => {
              item.style.backgroundColor = 'pink';
            })
          }
      

      查看https://stackblitz.com/edit/angular-services-example-ygmmaq?file=src/app/app.component.html

      【讨论】:

      • document.querySelector&lt;HTMLElement&gt;('.highlighted').style.backgroundColor = 'white' 中删除? `它说Object is possibly 'null'.ts(2531)
      • 试试最后一段代码。那可行。它有检查,比如它是否存在。
      • 我在forEach((item:HTMLElement) 上面试过上面的代码@它说Argument of type '(item: HTMLElement) =&gt; void' is not assignable to parameter of type '(value: Element, key: number, parent: NodeListOf&lt;Element&gt;) =&gt; void'. Types of parameters 'item' and 'value' are incompatible. Type 'Element' is missing the following properties from type 'HTMLElement': accessKey, accessKeyLabel, autocapitalize, dir, and 107 more.
      • @ParitoshM 我已经用 stackblits 链接更新了答案。请检查:)
      • @ParitoshM 先将 querySelector 的结果存储在一个变量中。
      【解决方案3】:

      未在文档对象上正确设置转换

      ngAfterViewInit() {
      setTimeout(() => {
      //this.tada = document.querySelectorAll('.highlighted').length;
      //you don't need this unless you are trying to apply the pink colour to all class with the selector above. returns an array of all elements with class highlighted
      let highlight: Array<HTMLElement> = <HTMLElement>document.querySelectorAll('.highlighted');
      //or let highlight:HTMLCollection[] = Array.from(document.getElementsByClassName('highlighted') as HTMLCollection<HTMLElement>)
      highlight.forEach((el:HTMLElement) => {
       if(!el) return;
      el.style.backgroundColor = 'pink';
      });
      ,0}
      }
      
      

      请参阅此链接了解更多信息 Typescript 3.7@beta Optional Chaining operator using problem

      【讨论】:

      • 我很困惑,你能不能把整个ngAfterViewInit() 代码,然后添加你的代码?
      • @ParitoshM 移除生命周期钩子不会改变静态类型检查错误。
      • 好的,但是如果我在其他函数中只定义了这一行,我应该怎么做?document.querySelector&lt;HTMLElement&gt;('.highlighted')?.style.backgroundColor = 'white';
      • 我改变了答案。您必须将 querySelector 分配给一个变量。
      • 如果我写如上&lt;HTMLElement&gt;document.querySelector('.highlighted')?.style.backgroundColor = 'white';`它说Property 'style' does not exist on type 'Element'.ts(2339)
      猜你喜欢
      • 2018-05-04
      • 2022-07-11
      • 2016-12-13
      • 2021-08-30
      • 2019-02-22
      • 1970-01-01
      • 1970-01-01
      • 2018-10-08
      • 2016-11-24
      相关资源
      最近更新 更多