You need to enable JavaScript to run this app. 如何从Angular库导出常规类/枚举?跨库引用报错解决
优惠活动
大模型
产品
解决方案
定价
更多

如何从Angular库导出常规类/枚举?跨库引用报错解决

解决Angular跨库导出/导入TypeScript类的步骤

1. 确认mat-text-editor-select的public-api.ts配置

检查mat-text-editor-select库根目录下的public-api.ts,确保已正确导出MatTextEditorSelectOption类及相关组件:

// mat-text-editor-select/public-api.ts
export * from './mat-text-editor-select-option';
export * from './mat-text-editor-select-component'; // 同步导出组件

2. 构建本地依赖并配置引用

因为是本地多库项目,不能直接跨项目引用源文件,需通过构建后的包依赖:

  • 先构建mat-text-editor-select库:
    ng build mat-text-editor-select
    
  • 在mat-text-editor项目的package.json中添加本地依赖:
    "dependencies": {
      "mat-text-editor-select": "file:../mat-text-editor-select/dist/mat-text-editor-select"
    }
    
  • 执行npm install让依赖生效。

3. 正确导入目标类

在mat-text-editor的代码中,直接从库名导入即可:

import { MatTextEditorSelectOption } from 'mat-text-editor-select';

4. 排查常见问题

  • 检查mat-text-editor-select的tsconfig.lib.json,确保include数组包含所有源文件:
    "include": [
      "src/**/*.ts"
    ]
    
  • 查看mat-text-editor-select/dist/mat-text-editor-select目录下的.d.ts文件,确认MatTextEditorSelectOption的类型定义已生成,且在public-api.d.ts中有导出。
  • 重启VS Code的TypeScript服务(快捷键Ctrl+Shift+P,选择"TypeScript: Restart TS Server"),清除缓存后重新验证。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 17:55:30