【问题标题】:Paths in an Angular libraryAngular 库中的路径
【发布时间】:2020-11-03 20:24:31
【问题描述】:

我看到一些地方说您可以在库中使用 tsconfig 路径。 我试图设置它,但我的项目无法编译。 我的服务的完整路径是 example-project/projects/example-project/src/lib/_core/services 所以我已将此添加到我的 tsconfig.ts 文件中(在脚文件夹中):

"paths": {
  "@core": ["src/app/_core"],
  "@configs": ["src/app/_core/configs"],
  "@models": ["src/app/_core/models"],
  "@services": ["src/app/_core/services"],
  "@validators": ["src/app/_core/validators"]
}

但是如果我尝试从我的库中引用一个服务,它将无法编译:

这是完整的课程:

import { ModuleWithProviders, NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

import { CloudinaryModule } from '@cloudinary/angular-5.x';
import * as Cloudinary from 'cloudinary-core';

import { IMAGES_CONFIG, ImagesConfig } from '@configs';
import { ResponsiveImageComponent } from './responsive-image.component';

@NgModule({
  declarations: [ResponsiveImageComponent],
  exports: [ResponsiveImageComponent, CloudinaryModule],
  imports: [CommonModule, CloudinaryModule],
})
export class ImagesModule {
  static forRoot(config: ImagesConfig): ModuleWithProviders {
    return {
      ngModule: ImagesModule,
      providers: [
        { provide: IMAGES_CONFIG, useValue: config },
        ...CloudinaryModule.forRoot(Cloudinary, {
          cloud_name: config.cloudName,
        }).providers,
      ],
    };
  }
}

有谁知道我做错了什么?

【问题讨论】:

  • 解决方案中有多少个模块?他们叫什么名字?

标签: angular angular-library


【解决方案1】:

我想通了。两件事。

忘记在目录下添加index.ts文件,例如:

export * from './core-config.token';
export * from './core-config';
export * from './images-config.token';
export * from './images-config';

通过查看带有路径的普通应用程序,我得到的第二件事。 tsconfig 中的 baseUrl 设置为“./”,因此路径必须从那里开始,所以我将 tsconfig 更改为:

"paths": {
  "@core": ["projects/example-project/src/app/_core"],
  "@configs": ["projects/example-project/src/app/_core/configs"],
  "@models": ["projects/example-project/src/app/_core/models"],
  "@services": ["projects/example-project/src/app/_core/services"],
  "@validators": [
    "projects/example-project/src/app/_core/validators"
  ]
}

现在可以使用了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-08
    • 2020-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多