英文:
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 ||
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论