首页 > 解决方案 > 扩展 BsPrefixComponent 进行自定义时找不到 react-bootstrap 模块

问题描述

我正在使用 Create-React-App、Typescript(3.4) 和 react-bootstrap (1.0 beta) 来构建网站。我创建了一个组件 CustomDropdown,它扩展了 BsPrefixComponent。BsPrefixComponent 是从 'react-bootstrap/helpers' 导入的。但是,这无法通过错误消息编译:

编译失败。

./src/CustomDropDown.tsx
未找到模块:无法解析 'path\to\my\project\bs\src' 中的 'react-bootstrap/helpers'

这是我的代码:

import React, { Component } from 'react';
import { BsPrefixComponent } from 'react-bootstrap/helpers';

interface IProps {
  name: string;
}
export default class CustomDropDown extends BsPrefixComponent<'div', IProps> {
  render() {
    return (
      <div>
        {this.props.name}
      </div>
    )
  }
}

奇怪的是,当使用手动编译时tsc --build tsconfig.js --traceResolution,我没有看到任何错误。我检查了模块解析日志,“react-bootstrap/helpers”已成功解析。

此外,我的 VSCode 编辑器中没有显示错误。

但是,使用npm startor时失败了npm run build

那么,这是自定义 react-bootstrap 组件的错误方法,还是存在错误?

标签: typescriptcreate-react-appreact-bootstrap

解决方案


推荐阅读