我一直在我的React应用程序中构建链接(使用4.3.1),使用如下代码:
const { match } = this.props;
...
pages.map(page =>
<Link to={match.url+'/'+page.id()+'/info'}>
Go to info page for {page.title()}
</Link>)因为这似乎是推荐的做法(例如,请参阅${match.url}/components https://reacttraining.com/react-router/web/guides/quick-start/example-nested-routing)。
我遇到的问题是:
如果我在下面的道路上:
/app/home上述链接如预期的那样:
但是,如果我加载这个(细微不同的)路径(注意后面/):
/app/home/然后生成的链接是错误的(注意双/回家后):
换句话说,问题是,有时有拖尾/有时没有。
当我构建一个链接时,是否需要手动检查拖尾/每次都需要删除它(如果它在那里)?还是有更好的最佳实践,我可能错过了?
发布于 2019-04-18 07:11:35
放弃用户提供的尾随/:
const {match} = this.props;
let len = match.url.length;
match.url = (match.url[len - 1] === "/") ? match.url.substring(0, len-1) : match.url;
...
pages.map(page =>
<Link to={match.url+'/'+page.id()+'/info'}>
Go to info page for {page.title()}
</Link>)或者,您也可以添加它是否也缺失:
match.url = (match.url[len - 1] === "/") ? match.url : match.url + "/";
pages....
<Link to={match.url + page.id() ....}
... 我认为添加一个缺失的一个比放弃一个现有的更容易。
https://stackoverflow.com/questions/55736642
复制相似问题