当运行 npm run build 并将我的 React 元素部署到 netlify 时,我网站的 netlify 版本丢失了一些引导样式...这是我的本地主机版本的图像: /image/FMUe2.png
这里是 netlify 版本的链接: https://ncnews-timdowd.netlify.com/
我已确保在index.html中添加相关样式表链接,并将相关导入添加到index.js中,按照react-bootstrap、bootstrap和CRA文档中的说明,不太确定出了什么问题。我发现如果删除该行:
导入'bootstrap/dist/css/bootstrap.css'
从我的index.js并在本地运行,我在本地版本中遇到了与我在netlify版本中完全相同的问题,所以这肯定与引导样式表未使其部署有关...
如果你想查看我的 github 存储库: https://github.com/timothydowd/FE-NC-News
package.json显示bootstrap和react-bootstrap版本:
{
"name": "front-end-nc-news",
"version": "0.1.0",
"private": true,
"dependencies": {
"@fortawesome/fontawesome-svg-core": "^1.2.17",
"@fortawesome/free-solid-svg-icons": "^5.8.1",
"@fortawesome/react-fontawesome": "^0.1.4",
"@reach/router": "^1.2.1",
"axios": "^0.18.0",
"bootstrap": "^4.3.1",
"css-loader": "^2.1.1",
"mdbreact": "^4.13.0",
"react": "^16.8.6",
"react-bootstrap": "^1.0.0-beta.8",
"react-dom": "^16.8.6",
"react-scripts": "2.1.8",
"style-loader": "^0.23.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
],
"devDependencies": {}
}
index.js:
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import 'bootstrap/dist/css/bootstrap.css';
import './index.css';
ReactDOM.render(<App />, document.getElementById('root'));
index.html 中的头:
<head>
<meta charset="utf-8" />
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico" />
<meta
name="viewport"
content="width=device-width, initial-scale=1, shrink-to-fit=no"
/>
<meta name="theme-color" content="#000000" />
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<title>NC NEWS</title>
<link
rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T"
crossorigin="anonymous"
/>
</head>
如前所述..
预期结果是我在开发服务器中看到的结果:/image/FMUe2.png
实际结果可以在我部署的构建中看到,但缺少样式: https://ncnews-timdowd.netlify.com/
最佳答案
问题在于 cra 构建 css 的顺序与在开发环境中编译 css 的顺序相比。通过修改 app.css 文件并删除一些不必要的 css 修复了该问题。
关于css - 为什么我在使用 create-react-app 的生产构建中丢失了 Bootstrap 样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56300299/