我有一个新的带有 webpack 的 React 项目,我正在尝试向它添加“react-bootstrap”并呈现一个警报作为测试。
所以我已经完成了:
npm install react-bootstrap --save
并已验证文件在那里。在我的主要 tsx 文件中,我有:
import { Alert } from 'react-bootstrap';
...
<Alert variant='success'>Success Alert</Alert>
当我运行 npm start 来启动我的服务器时,它会呈现带有成功警报的页面,但它会出现带有以下错误的 webpack 错误提示:
[tsl] ERROR in ....\src\client-react\components\Header.tsx(9,23)
TS2307: Cannot find module 'react-bootstrap'.
即使警报在下方正确显示(并且如果我破解 css 以显示:在错误启动时没有显示,它在下方看起来绝对没问题?
我认为这是因为 react-bootstrap 包没有任何@types(@types/react-bootstrap 是针对旧版本的,因为他们显然已经将类型滚动到主包)但我不确定如何修复。
谢谢!
最佳答案
我正在查看 react-bootstrap
中提供的示例使用 typescript 。这里的方法是在 index.tsx
中导入包为:
import "bootstrap/dist/css/bootstrap.min.css";
我希望你有 react-app-env.d.ts
文件来引用 react-scripts
类型,如果没有创建并添加它会自动生成
/// <reference types="react-scripts" />
所以在您的 Hello.tsx 中,
您可以将 Alert
从 react-bootstrap
导入为:
import Alert from 'react-bootstrap/Alert';
关于reactjs - TS2307 : Cannot find module 'react-bootstrap' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57925267/