Bootstrap 的 JS 无法在 React 中运行/加载

问题描述 投票:0回答:1

我安装了一个基本的 React 应用程序

npx create-react-app appnamehere

然后我安装了 bootstrap(也尝试了 v4 和 v5)

npm install bootstrap

然后将它们加载到我的

App.js

import "bootstrap/dist/css/bootstrap.min.css";
import "bootstrap/dist/js/bootstrap.min";

css
可以工作,但是
js
组件(如警报、折叠等)不起作用。发现这是因为
js
文件没有导入到我的项目中 - 这很奇怪,因为我实际上导入了它们。我尝试了
CDN
方法,用这个来改变我的
index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="theme-color" content="#000000" />
    <meta
      name="description"
      content="Web site created using create-react-app"
    />
    <link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
    <link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
    <title>MyApp</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
  </head>
  <body>
    <noscript>You need to enable JavaScript to run this app.</noscript>
    <div id="root"></div>
    <script
      src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
      integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
      crossorigin="anonymous"
    ></script>
    <script
      src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js"
      integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49"
      crossorigin="anonymous"
    ></script>
    <script
      src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"
      integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy"
      crossorigin="anonymous"
    ></script>
  </body>
</html>
 

你猜怎么着,同样的事情。

CSS
就像魅力一样,
JS
东西不起作用。有什么想法吗?

javascript html reactjs bootstrap-4 bootstrap-5
1个回答
0
投票

如果你想将它与React一起使用,我建议使用react-bootstrap。 https://react-bootstrap.netlify.app/

npm install react-bootstrap bootstrap

您可以查看他们的文档以了解如何使用它。就像导入组件一样。

© www.soinside.com 2019 - 2024. All rights reserved.