【问题标题】:All styling (bootstrap, css) goes away when a specific component is rendered and refreshed in the browser当特定组件在浏览器中呈现和刷新时,所有样式(引导程序、css)都会消失
【发布时间】:2020-04-17 15:01:40
【问题描述】:

我遇到了一个组件的问题,当它在浏览器中呈现时,所有样式都会被删除,包括引导 css 和 js。当我走到一条特定的路径时,就会发生这种情况:

http://localhost:3000/profile/5e85e1ce61fb132564b2e315

如果我通过单击应用程序假定的超链接访问此链接,则第一次加载正常。当我刷新它时,所有样式都消失了。 其他路径和组件不是这样,无论刷新多少次都可以正常渲染。

当它发生时,我会在控制台中收到这些错误:

Refused to apply style from 'http://localhost:3000/profile/assets/css/bootstrap.min.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
Refused to apply style from 'http://localhost:3000/profile/assets/css/mdb.min.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
Refused to apply style from 'http://localhost:3000/profile/assets/css/styles.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.

Failed to load resource: the server responded with a status of 404 (Not Found)
popper.min.js:1 Failed to load resource: the server responded with a status of 404 (Not Found)
bootstrap.min.js:1 Failed to load resource: the server responded with a status of 404 (Not Found)
mdb.min.js:1 Failed to load resource: the server responded with a status of 404 (Not Found)

我将这些链接放在我的 public/index.html 文件中。下面是它的外观:

<!-- Bootstrap core CSS -->
    <link href="./assets/css/bootstrap.min.css" rel="stylesheet">
    <!-- Material Design Bootstrap -->
    <link href="./assets/css/mdb.min.css" rel="stylesheet">
    <!-- Your custom styles (optional) -->
    <link href="./assets/css/styles.css" rel="stylesheet">

<!-- SCRIPTS -->
    <!-- JQuery -->
    <script type="text/javascript" src="./assets/js/jquery.min.js"></script>
    <!-- Bootstrap tooltips -->
    <script type="text/javascript" src="./assets/js/popper.min.js"></script>
    <!-- Bootstrap core JavaScript -->
    <script type="text/javascript" src="./assets/js/bootstrap.min.js"></script>
    <!-- MDB core JavaScript -->
    <script type="text/javascript" src="./assets/js/mdb.min.js"></script>

我不明白为什么它会尝试去路径:http://localhost:3000/profile/assets/css/styles.css 来获取这些文件,而不是我在索引文件中给出的路径。它适用于 Web 应用程序的其余部分,而不是这个特定的路径和组件。

【问题讨论】:

  • 因为您似乎正在对 index.html 的任何路径进行服务器重定向,这意味着对于浏览器,您网站的内容根目录是 /profile/,因此所有相关资产都应该加载从他们./assets/... 转换为/profile/assets/
  • 但我不知道怎么做。我没有从我的操作文件甚至组件的 React 代码中看到这种重定向。

标签: javascript node.js reactjs redux


【解决方案1】:

通过在 index.html 文件的 head 部分的链接之前添加以下内容解决了问题:

<base href="/">

【讨论】:

    猜你喜欢
    • 2012-07-30
    • 2018-11-13
    • 1970-01-01
    • 2017-10-01
    • 1970-01-01
    • 2015-11-20
    • 2015-05-05
    • 2015-02-18
    • 2011-08-22
    相关资源
    最近更新 更多