【问题标题】:React Router redirecting href of CSS反应路由器重定向CSS的href
【发布时间】:2016-05-06 20:55:11
【问题描述】:

我的 routes.js 设置如下:

<Route path="users" component={requireAuth(users)} />
<Route path="user-:id" component={requireAuth(userSelect)} />

一切正常,当我将破折号更改为正斜杠时,我唯一的问题出现了:

<Route path="user/:id" component={requireAuth(userSelect)} />

当我这样做时,我的 css 的 href 路径会发生变化,所以当我打开 www.siteexample.com/user/1 时,我会收到如下所示的 GET 错误:

GET http://siteexample.com/user/style/css/bootstrap.min.css 

我的 index.html 看起来像这样:

<link href="style/css/bootstrap.min.css" rel="stylesheet">

似乎当添加正斜杠时,“用户”成为相对文件路径的根,并且反应路由器试图从那里找到 css 文件而不是 index.html 的位置。链接href实际上应该是

 http://siteexample.com/style/css/bootstrap.min.css 

有什么我可以添加到我的 href 以强制它进入我网站的主根目录的吗?

【问题讨论】:

  • 你不能像 href="/style/css/bootstrap.min.css" 那样链接到你的css文件吗?
  • @nuway 成功了,谢谢!
  • 不客气,请接受下面的回答

标签: react-router href relative-path


【解决方案1】:

您可以将 CSS 链接到网站的根目录,例如 href="/style/css/bootstrap.min.css"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-22
    • 2018-08-13
    • 2017-10-22
    • 1970-01-01
    • 1970-01-01
    • 2018-12-25
    • 2021-09-29
    • 1970-01-01
    相关资源
    最近更新 更多