【问题标题】:VSCode does not suggest auto import when re-exporting a default export in an index file重新导出索引文件中的默认导出时,VSCode 不建议自动导入
【发布时间】:2021-01-27 12:34:15
【问题描述】:

我正在开发一个 React 应用程序并来到这个案例。

输入

  • 文件夹结构:

    /ProductTypeSelection
      index.tsx
      ProductTypeSelectionView.tsx
    
  • ProductTypeSelectionView.tsx:

    const ProductTypeSelectionView: React.FunctionComponent<ProductTypeSelectionViewProps> = () => {
     return <div />;
    };
    
    export default ProductTypeSelectionView;
    
  • index.tsx:

    export { default } from './ProductTypeSelectionView'
    

期望的输出
现在我可以像这样导入 ProductTypeSelection 组件:import ProductTypeSelection from 'src/ProductTypeSelection' 但 VSCode 不建议自动导入 ProductTypeSelection(只是 ProductTypeSelectionView)。

所以我想要的输出将能够使用该导入语法并同时从 VSCode 获得 ProductTypeSelection 导入建议。我想知道这是否可以实现,非常感谢。

试过
这将按预期工作,但想知道我是否可以在 1 行中重写它。

import ProductTypeSelection from "./ProductTypeSelectionView";

export default ProductTypeSelection;

【问题讨论】:

    标签: reactjs visual-studio-code import export intellisense


    【解决方案1】:

    您导出default,改为尝试:

    export { default as ProductTypeSelectionView } from './ProductTypeSelectionView'
    

    【讨论】:

    • 使用这种方式导入语法将是import { ProductTypeSelection } from 'src/ProductTypeSelection'。我想知道我是否可以保留这种语法import ProductTypeSelection from 'src/ProductTypeSelection'
    • 你不能..除非你正常导入并默认导出。 import Product, export default Product
    • 谢谢,我试过了,我会更新问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-08
    • 2012-11-03
    • 1970-01-01
    • 2021-10-12
    • 2022-12-07
    • 2019-12-06
    • 2022-11-02
    相关资源
    最近更新 更多