Javascript 反应路由器dom中location.pathname和match.url之间的差异?
Javascript 反应路由器dom中location.pathname和match.url之间的差异?,javascript,reactjs,react-router,react-router-v4,react-router-dom,Javascript,Reactjs,React Router,React Router V4,React Router Dom,props.location.pathname和props.match.url 在react路由器dom中 从他们的文档中: match.url (字符串)URL的匹配部分。用于构建嵌套的s 位置 用于匹配子元素而不是当前历史位置(通常是当前浏览器URL)的位置对象 到目前为止,我只见过它们有完全相同的值 示例: 如果我的路线在此url中匹配: /search/searchValue?category=无论什么 我想删除查询字符串并转到: /search/searchValue 我应该使用一个而
props.location.pathname
和props.match.url
在react路由器dom中
从他们的文档中:
match.url
(字符串)URL的匹配部分。用于构建嵌套的
s
位置
用于匹配子元素而不是当前历史位置(通常是当前浏览器URL)的位置对象
到目前为止,我只见过它们有完全相同的值
示例:
如果我的路线在此url中匹配:
/search/searchValue?category=无论什么
我想删除查询字符串并转到:
/search/searchValue
我应该使用一个而不是另一个,还是两者都可以使用?
位置。路径名
表示根相对url
match.url
表示url的匹配部分,因此可能是location.pathname
的一部分
鉴于这两个组成部分:
function Home({match, location}) {
return (
<div>
{match.url}
<br/>
{location.pathname}
</div>
);
}
function App() {
return (
<Router>
<Route path="/" component={Home}/>
</Router>
);
}
函数主目录({match,location}){
返回(
{match.url}
{location.pathname}
);
}
函数App(){
返回(
);
}
如果您转到/something
,则
- 匹配。url将是/(因为url的匹配部分是
)/
- 位置。路径名将是/something(相对根URL)
/search/searchValue
),那么您应该使用匹配.url
,因为位置.pathname
可能大于/search/searchValue
->/search/searchValue/something