如何在React中接受或检查`router.pathname.startsWith`中的多个链接

huangapple go评论74阅读模式
英文:

How to accept or check multiple links in router.pathname.startsWith in REACT

问题

我如你所愿,只返回翻译好的部分,不包含其他内容:

你可以如何检查多个URL链接与router.pathname.startsWith
目前我正在使用

router.pathname.startsWith("/blogs/", "/webinars/")

但它只检查第一个链接"/blogs/"。

英文:

How can I check multiple URL links with router.pathname.startsWith ?

currently im using

router.pathname.startsWith("/blogs/", "/webinars/")

but its only checking the first link "/blogs/"

答案1

得分: 1

第二个参数在String.prototype.startsWith()方法中用于位置,因此您必须这样使用它:

if (router.pathname.startsWith("/blogs/") || router.pathname.startsWith("/webinars/"))
英文:

The second parameter in String.prototype.startsWith() method is for postion so you have to use it this way :

if (router.pathname.startsWith("/blogs/") || router.pathname.startsWith("/webinars/"))

答案2

得分: 0

如果要检查路径名中是否包含/blogs/ /webinars/,你可以像这样做:

if (router.pathname.includes("/blogs/") || router.pathname.includes("/webinars/")) { // 你的代码... }

但如果要检查路径名中是否同时包含/blogs/ /webinars/,你可以像这样做:

if (router.pathname.includes("/blogs/") && router.pathname.includes("/webinars/")) { // 你的代码... }

区别在于&&||

英文:

If you want to check if /blogs/ or /webinars is included in pathname, you can do something like this

if (router.pathname.includes("/blogs/") || router.pathname.includes("/webinars/")) { // your code... }

But if you want to check if /blogs/ and /webinars/ are included in pathname, you can do something like this

if (router.pathname.includes("/blogs/") && router.pathname.includes("/webinars/")) { // your code... }

The difference is just && or ||

huangapple
  • 本文由 发表于 2023年3月15日 18:12:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75743275.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定