javascript - React Router - 添加变量路由时样式表被标记为 "text/html"(/:userId)

标签 javascript express reactjs react-router

我们的自定义样式表“styles.css”被标记为错误的 MIME 类型“text/html”,甚至认为它有:

rel="stylesheet" type="text/css"

此错误仅在使用可变参数(如/:userId)进行路由时发生

准确的错误是:

The stylesheet http://localhost:3000/user/static/styles/styles.css was not loaded because its MIME type, “text/html”, is not “text/css”.

路由器代码:

ReactDom.render(
  <Router history={browserHistory}>
    <Route component={MainLayout}>
      <Route path="/" component={App} />
      <Route path="/main" component={Home} />
      <Route path="/user/:userId" component={UserSetting} />
    </Route>
  </Router>,
  document.querySelector('.container')
);

这可能只是我需要在我们的快速服务器上考虑的一些配置吗?

最佳答案

事实证明这是我的一个简单疏忽。在我的 index.html 中,我从 static 开始的路径缺少斜线。

<link rel="stylesheet" type="text/css" href="static/styles/styles.css">

<link rel="stylesheet" type="text/css" href="/static/styles/styles.css">

关于javascript - React Router - 添加变量路由时样式表被标记为 "text/html"(/:userId),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39863781/

相关文章:

node.js - 如何强制刷新 req.user?

node.js - 我需要为要接受数组的get创建url,如何在node.js/express中从请求中提取数组?

node.js - 如何默认将错误返回为 JSON 而不是带有 express 的 HTML?

javascript - React 功能组件中的 Prop 名称

javascript - 计算和删除 JSON 数据

Javascript 数值库 - 向量、矩阵、确定、求逆

javascript - Alert() 在 else 语句中无法正常工作

IE 中的 JavaScript Math.round 错误

php - php问题中的持久数据

reactjs - 在react中动态添加组件