网站搜索

reactjs - react-bootstrap 模块没有导出的成员


我想在我的 Layout 组件中使用 npm 模块 react-bootstrap (https://www.npmjs.com/package/@types/react-bootstrap)(就像 ASP.NET Core 2.1 模板客户端项目中一样)。但模板中的文件扩展名是 .js。我希望使用 Typescript,所以 Layout.tsx 看起来像这样:

import * as React from 'react';
import { Col, Grid, Row } from 'react-bootstrap/';           <----- compile time error: Module has no exported member col / grid / row
import NavMenu from './NavMenu';

interface Layout {
    children: any;
}

const Layout = ({ children }) => {
    return (<Grid fluid>
        <Row>
            <Col sm={3}>
                <NavMenu/>
            </Col>
            <Col sm={9}>
                {this.props.children}
            </Col>
        </Row>
    </Grid>);
}
export default Layout;

我的 package.json 文件看起来是这样的:

{
  "name": "MyApp",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@stitches/react": "^1.2.5",
    "@types/react-bootstrap": "^0.32.28",
    "@types/react-dom": "^17.0.11",
    "@types/react-router": "^5.1.17",
    "@types/react-router-bootstrap": "^0.24.5",
    "bootstrap": "^3.4.1",
    "react": "^17.0.2",
    "react-beautiful-dnd": "^13.1.0",
    "react-bootstrap": "^2.0.2",
    "react-dom": "^17.0.2",
    "react-router": "^6.0.0",
    "react-router-bootstrap": "^0.24.4",
    "react-router-dom": "^4.2.2",
    "react-scripts": "1.0.17",
    "rimraf": "^2.6.2"
  },
  "scripts": {
    "start": "rimraf ./build && react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  },
  "devDependencies": {
    "@types/react": "^17.0.34",
    "css-loader": "^6.5.1",
    "style-loader": "^3.3.1"
  }
}

我还在 index.tsx 中导入了 'bootstrap/dist/css/bootstrap.css' 和 'bootstrap/dist/css/bootstrap-theme.css'。

更多信息:

  • ECMAScript 版本:ECMAScript 2017
  • TSX 文件中的 JSX 编译:React
  • 模块系统:ES2015

有人知道问题可能出在哪里吗?我刚接触 React,所以谢谢你的支持!

答案#1

react-bootstrap 没有 Grid 组件,而是有 Container 组件。除此之外,你不应该在函数组件中使用 this.props.children。所以,你的代码应该像这样:

import React from 'react';
import Col from "react-bootstrap/Col";
import Container from "react-bootstrap/Container";
import Row from "react-bootstrap/Row";
import NavMenu from './NavMenu';

interface Layout {
    children: any;
}

const Layout = ({ children }) => {
    return (<Container fluid>
        <Row>
            <Col sm={3}>
                <NavMenu/>
            </Col>
            <Col sm={9}>
                {children}
            </Col>
        </Row>
    </Container>);
}
export default Layout;

你可以看看这个沙盒以获取一个实时工作示例。