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

Angular 15类库多路径导入配置失败,如何解决模块找不到问题?

Angular类库子路径导入配置失败问题

项目说明

  • testmultiimports-example是我的类库的通用测试项目
  • 目标:实现类库的子路径导入(如import { Service1 } from 'testmultiimports/path1'),但使用通配符配置路径映射后无效

当前代码(testmultiimports-example/src/app/app.component.ts)

import { Component } from '@angular/core';

// 当前可正常工作的导入方式
// import { Service1, Service2 } from 'testmultiimports';

// 期望实现的导入方式(报错)
import { Service1 } from 'testmultiimports/path1';
import { Service2 } from 'testmultiimports/path2';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html'
})
export class AppComponent {
  
  public Service1Result: string;
  public Service2Result: string;

  constructor(s1: Service1, s2: Service2){
    this.Service1Result = s1.getResult();
    this.Service2Result = s2.getResult();
  }
}

错误提示

新导入模式下找不到模块'testmultiimports/path1'或其对应的类型声明

相关配置文件

根目录tsconfig.json

{
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",   
    "target": "ES2022",
    "module": "ES2022",    
    "paths": {
      // "testmultiimports": ["dist/testmultiimports"],
      "testmultiimports/*": ["dist/testmultiimports/*"]
    }
}

angular.json相关配置

"tsConfig": "projects/testmultiimports-example/tsconfig.app.json"

testmultiimports-example的tsconfig.app.json

{
  "extends": "../../tsconfig.json",
  "compilerOptions": {
    "outDir": "../../out-tsc/app",
    "types": []
  },
  "files": [
    "src/main.ts"
  ],
  "include": [
    "src/**/*.d.ts"
  ]
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 22:17:32