样式表不加载,因为MIMEtypes是text / html
当在Firefox上运行MERN应用程序时,在浏览器控制台中得到了这个错误,而且css没有加载: The stylesheet http://localhost:3000/src/css/component.css was not loaded because its MIME type, “text/html”, is not “text/css”
我将CSS添加到index.html
如下所示:
<link rel="stylesheet" type="text/css" href="../src/css/component.css" />
我哪里错了?
除了React组件之外,还有哪些其他方法可以添加外部CSS?
代码在这里
样式表http:// localhost:3000 / src / css / component.css未加载,因为其MIMEtypes“text / html”不是“text / css”
错误的原因是,
在浏览器中,您只能访问公共目录,所以
– >第一个../src/css/
通过这种方式你不能访问这个文件,它会把这个当做路由,并且试着给你html
– >其次,这不是包含css文件的正确方法:
<link rel="stylesheet" type="text/css" href="../src/css/normalize.css" /> <link rel="stylesheet" type="text/css" href="../src/css/demo.css" /> <link rel="stylesheet" type="text/css" href="../src/css/component.css" />
porper的方式来使用css文件是这样的(从你的反应组件js文件):
import './css/component.css';
( react-scripts start
)React会自动将你的css文件转换成js并应用它。
如果你想使用css文件之外的反应,你需要把所有的css文件在公共文件夹(好放在公共/ css里面)
<link rel="stylesheet" type="text/css" href="css/normalize.css" /> <link rel="stylesheet" type="text/css" href="css/demo.css" /> <link rel="stylesheet" type="text/css" href="css/component.css" />
如果您仍然有疑问,请阅读:
https://github.com/facebookincubator/create-react-app/blob/master/README.md#getting-started
希望,这将清除你所有的疑惑。
我将文件夹css和js移动到公共目录,并将component.css
引用为
<link rel="stylesheet" type="text/css" href="%PUBLIC_URL%/css/component.css" />
它为我工作..
任何更好的方法?