【问题标题】:Angular:Unable to detect a typescript artifact from another angular libraryAngular:无法从另一个 Angular 库中检测到打字稿工件
【发布时间】:2018-11-27 15:16:07
【问题描述】:

我已经制作了一个角度库(A)。我从“ng generate”命令开始。

所以它在 lib 文件夹中为我创建了类。我把我的实现放在这些类中。除了这些自动生成的类之外,我还在 lib 中创建了另外两个文件夹。一个包含我的服务类(@Injectable),另一个(名为“模型”)包含我的数据类(包含在文件 model.ts 中),它们是普通的 ts 类。

所以对于这些数据类,文件夹结构是:

lib/model/model.ts

在 app.module.ts 中,我将这些类(数据类除外)放在适当的组下,如导入、提供程序等。

现在我在 Angular 项目中使用 A,比如“my-current-angular-project”。我在 package.json 中添加了依赖,并在 node_modules 中添加了库。

在这个项目中,我这样做:

import {X, Y} from 'A/lib/model/model';

其中 X,Y 是我在 A(/lib/model/model.ts) 中的 model.ts 中定义的数据类。

但是当我为这个项目做'ng build'时,我得到了错误:

./src/app/app.component.ts 中的错误

找不到模块:错误:无法解析“D:\projects\my-current-angular-project\src\app”中的“A/lib/model/model”

我在这里犯了什么错误?

【问题讨论】:

    标签: angular typescript


    【解决方案1】:

    我不太确定我的文件夹结构/问题是否正确。但是,以下文件夹结构应该可以工作:

    • 您所有来源的根文件夹my-current-angular-project\src\
    • 在此文件夹中,您可以将数据类(例如 my-current-angular-project\src\my_data_class.ts)放置在您定义 export class Data { ... 的位置
    • 现在让我们假设您已经使用 angular cli 生成了一个组件,例如 my-component
    • 您现在有一个文件夹my-current-angular-project\src\my-component\,其中包含文件my-component.component.ts
    • 在此文件 (my-component.component.ts) 中,您现在可以像这样使用您的数据类:import {Data } from '../my_data_class';

    注意:在这种情况下,无需在 angular.json 或其他地方指定/包含 my_data_class.ts

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-21
      • 2017-07-22
      • 1970-01-01
      • 1970-01-01
      • 2018-04-15
      • 1970-01-01
      • 2013-02-25
      • 2022-08-12
      相关资源
      最近更新 更多