You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

首次使用Storybook:父组件渲染时报子组件接口未导出错误

问题排查与解决步骤

针对StatsBar和Comment组件渲染报错(提示子组件接口导出不存在,但子组件单独在Storybook可正常展示),可从以下方向逐一排查:

1. 核对接口导入与导出的匹配性

确认父组件(如StatsBar)中导入子组件接口的语法,与子组件interface.ts的导出方式完全一致:

  • 若子组件接口为命名导出:
    // AuthenticationModal/interface.ts
    export interface AuthenticationModalProps { /* 接口定义 */ }
    
    父组件必须使用命名导入:
    // StatsBar组件内
    import { AuthenticationModalProps } from '../AuthenticationModal/interface';
    
    禁止误用默认导入(import AuthenticationModalProps from '...')。
  • 若子组件使用默认导出,需同步调整父组件的导入语法。

2. 验证相对路径准确性

检查父组件中导入子组件interface.ts的相对路径是否正确,避免因目录层级变化导致路径错误。例如StatsBar与AuthenticationModal同级时,路径应为./AuthenticationModal/interface.ts而非../。

3. 清理Storybook缓存并重启

Storybook缓存可能残留旧的模块导出信息,执行以下命令清理后重启:

# 清理缓存
npx storybook clean
# 重启Storybook服务
npm run storybook

4. 同步组件库与Storybook的解析配置

若组件库使用tsconfig.json设置了baseUrl或paths别名,需在Storybook配置文件(.storybook/main.ts)中同步配置:

// .storybook/main.ts
import { mergeConfig } from 'vite';
import path from 'path';

export default {
  // 其他Storybook配置
  viteFinal: async (config) => {
    return mergeConfig(config, {
      resolve: {
        alias: {
          '@': path.resolve(__dirname, '../src'), // 与tsconfig.json的别名保持一致
        },
      },
    });
  },
};

同时确认构建工具(Vite/Rollup)已将interface.ts纳入编译范围,未被排除或忽略。

5. 检查父组件Story文件的依赖加载

确认父组件的Story文件中,是否正确引入了子组件的所有依赖(包括类型定义),避免因Story配置缺失导致依赖无法解析。


内容的提问来源于stack exchange,提问作者Tsabary

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 20:50:13