如何从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
相关产品推荐
相关产品推荐

