Angular 使用构造函数传递参数时,Libray 构建失败并出现错误

Angular Libray build fails with error while pass the params with constructor

这是我的图书馆服务:

从“@angular/core”导入{可注射};

@可注射({ 提供于:'root' })

export class SharedService {

    name: string;

    constructor(name) { 
        this.name = name;
    }

}

当我像这样构建时:ng build 出现错误:

Compiling TypeScript sources through ngc
Warning: Can't resolve all parameters for SharedService in C:/722333/Tutorials/my-workspace/projects/ibo-shared-lib/src/lib/services/shared.service.ts: (?). This will become an error in Angular v6.x
Warning: Can't resolve all parameters for SharedService in C:/722333/Tutorials/my-workspace/projects/ibo-shared-lib/src/lib/services/shared.service.ts: (?). This will become an error in Angular v6.x
ERROR: Can't resolve all parameters for SharedService in C:/722333/Tutorials/my-workspace/projects/ibo-shared-lib/src/lib/services/shared.service.ts: (?).

如何解决这个问题?处理这个问题的正确方法是什么? 提前致谢。

你必须像下面那样在你的构造函数中使用@inject

export class SharedService {

name: string;

constructor(@inject('name') name) { 
    this.name = name;
}

}

但是您必须根据 https://angular.io/guide/dependency-injection-providers(useValue / useExisting 类型的注入)

在您的模块中提供具有 'name' 的依赖项提供程序