我刚刚使用 React cli 创建了一个新项目并安装了 npm bootstrap。
npm install bootstrap --save
然后我在 App.js 中尝试了 bootstrap:
import React, { Component } from 'react';
import './App.css';
class App extends Component {
render() {
return (
<div>
<div className="container">
<h1>It Works!</h1>
</div>
</div>
);
}
}
export default App;
但它不起作用。
这是我的 package.json:
{
"name": "myapp",
"version": "0.1.0",
"private": true,
"dependencies": {
"bootstrap": "^3.3.7",
"react": "^15.5.4",
"react-dom": "^15.5.4"
},
"devDependencies": {
"react-scripts": "1.0.7"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
}
这是我的index.html:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1>
<title>React App</title>
</head>
<body>
<div id="root"></div>
</body>
</html>
为了使其正常工作,我还需要做什么吗?
最佳答案
您安装了 bootstrap
npm 模块,但这仅将文件下载到您的 node_modules/
目录。您实际上还没有在项目本身中需要 Bootstrap 。
尝试在上面执行此操作导入'./App.css'
import 'bootstrap/dist/css/bootstrap.css';
如果导入不起作用,您还可以尝试链接到 public/index.html
中的 bootstrap.min.css
文件。
关于javascript - npm Bootstrap 不适用于新的 React 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44295183/