简体   繁体   English

如果 querySelector 不返回 null,则简写获取 textContent

[英]Shorthand get textContent if querySelector doesn't return null

I was try trying to find a shorthand way to do something like this我试图找到一种速记方式来做这样的事情

let text = el.querySelector('.some-class').textContent || ''

However the above gives an error:但是上面给出了一个错误:

Object is possibly 'null'.对象可能为“空”。

So I randomly decided to try this, and I get no errors any more.所以我随机决定尝试这个,我再也没有错误了。 However, I don't know what it is doing or if it is doing what I want it to do.但是,我不知道它在做什么,或者它是否在做我想要它做的事情。

let text = el.querySelector('.some-class')!.textContent || ''

Basically I am looking for a way to trigger the textContent part if an element is found otherwise trigger the part after the ||基本上我正在寻找一种方法来触发textContent部分,如果找到一个元素,否则触发||之后的部分. .

I am not sure what the expression I just wrote is, so I don't really know what to Google here.我不确定我刚刚写的表达式是什么,所以我真的不知道在这里谷歌什么。

What I think it is saying is to not evaluate the expression with a null result like when defining a type:我认为它的意思是不要像定义类型时那样用空结果来评估表达式

public value!: string

If I am correct is there a proper way to do what I would like?如果我是正确的,是否有正确的方法来做我想做的事?

You could define a utility function:你可以定义一个效用函数:

function getElText(query: string): string {
  const el = document.querySelector(query);
  return el && el.textContent || '';
}

Once optional chaining gets added to JS, you will likely be able to do this in TS/JS:一旦将可选链添加到 JS,您可能就可以在 TS/JS 中执行此操作:

let text = el.querySelector('.some-class')?.textContent || ''

In your working example, the !.在您的工作示例中, !. is telling TS to ignore the possible null type, so you can access properties on el , but if querySelector returns null , you will get a runtime error.告诉 TS 忽略可能的null类型,因此您可以访问el属性,但如果querySelector返回null ,您将收到运行时错误。

TypeScript is warning you that el.querySelector('.some-class') may return null . TypeScript 警告您el.querySelector('.some-class')可能返回null Essentially, if for whatever reason no element in your DOM matches that selector query (.some-class), you'll get null back.本质上,如果您的 DOM 中没有任何元素与该选择器查询 (.some-class) 匹配,您将返回null Then if you try to read the .textContent of null, that'll throw an exception.然后,如果您尝试读取 null 的.textContent ,则会引发异常。 TypeScript is fore-warning you about this possibility. TypeScript 预先警告你这种可能性。

To achieve the effect you desire, you can do something like:为了达到您想要的效果,您可以执行以下操作:

let element = el.querySelector('.some-class');
let text = element ? element.textContent : "";

As far as why putting a !至于为什么放一个! got rid of the error message, that's TypeScript's bang non-null-assertion operator described here .摆脱了错误消息,即此处描述的 TypeScript 的 bang 非空断言运算符。 With the !随着! you're essentially telling TypeScript not to worry about the null possibility.你本质上是在告诉 TypeScript 不要担心 null 的可能性。 It's useful when you, as the developer with knowledge of other parts of your application, can assert that a parameter won't be null.当您作为了解应用程序其他部分的开发人员可以断言参数不会为空时,这很有用。

可选链接无效合并typescript 3.7 的一部分,因此您现在可以使用:

let text = el.querySelector('.some-class')?.textContent ?? ''

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM