i want to hide my navbar when i'm in all page have path='/admin/...' but it's so length. How can i clean it, thank very much.
let HideHeader = window.location.pathname === '/admin/info' && '/admin/setting' && '/admin/post' && '/admin/messange' ? null : <MyNav username={username} />
You can use indexOf :
let HideHeader = window.location.pathname.indexOf('/admin/') === 0 ? null : <MyNav username={username} />
It will test for pathname that starts with /admin/
尝试使用includes()
方法:
let HideHeader = window.location.pathname.includes('/admin/') ? null : <MyNav username={username} />
您可以将匹配方法与正则表达式一起使用
let HideHeader = !window.location.pathname.match(/\/admin.*/g) && <MyNav username={username} />
You can use simple string methods like startsWith()
, search()
, indexOf()
, includes()
https://www.w3schools.com/jsref/jsref_search.asp
https://www.w3schools.com/jsref/jsref_startswith.asp
https://www.w3schools.com/jsref/jsref_indexof.asp
startsWith
window.location.pathname.startsWith('/admin')
This will return true if your URL starts with /admin
search
(window.location.pathname.search('/admin') !== -1)
This will return true if your URL contains /admin
indexOf
(window.location.pathname.indexOf('/admin') !== -1)
This will return true if your URL contains /admin
最有效、语义正确且易于理解的解决方案 - startsWith :
window.location.pathname.startsWith('/admin');
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.