从反应索引链接到 css 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44702061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Linking to css files from react index
提问by Remis07
I have this simple index :
我有这个简单的索引:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<title>SB Admin - Bootstrap Admin Template</title>
<!-- Auth0Lock script -->
<script src="//cdn.auth0.com/js/lock-9.1.min.js"></script>
<!-- Bootstrap Core CSS -->
<link href="/ui/css/bootstrap.min.css" rel="stylesheet" />
<!-- Custom CSS -->
<link href="/ui/css/sb-admin.css" rel="stylesheet" />
<!-- Custom Fonts -->
<link href="/ui/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css" />
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div id="root">
</div>
<script src="/static/bundle.js"></script>
</body>
</html>
But for some reason, after running npm start, it doesn't get access to the css files.. so i get a page without style. Here is the tree of my project:
但是出于某种原因,在运行 npm start 后,它无法访问 css 文件..所以我得到了一个没有样式的页面。这是我的项目树:
- actions
- components
- containers
- dist
- middleware
- node_modules
- reducers
- store
- ui
- --css
- --font-awesome
- index.html
- index.js
- webpack.config.js
- package.json
- 行动
- 成分
- 集装箱
- 区
- 中间件
- 节点模块
- 减速机
- 店铺
- 用户界面
- --css
- --font-awesome
- 索引.html
- 索引.js
- webpack.config.js
- 包.json
回答by Mμ.
If you want to include css in your html, you gotta put them in your publicfolder and use '%PUBLIC_URL%' as the path to your css. So it would look something like this:
如果你想在你的 html 中包含 css,你必须把它们放在你的公共文件夹中,并使用 '%PUBLIC_URL%' 作为你的 css 的路径。所以它看起来像这样:
<link href="%PUBLIC_URL%/bootstrap.min.css" rel="stylesheet" />
Alternatively, it is highly recommended that you do import the css that you want inside your react component, because your css would get minified and bundled. So you could do something like:
或者,强烈建议您在 react 组件中导入所需的 css,因为您的 css 会被缩小和捆绑。所以你可以这样做:
import React, { Component } from 'react';
// ... rest of imports
import './path/to/css/font-awesome.min.css'
For more info on this, go to the documentation here.
有关这方面的更多信息,请转到此处的文档。