【发布时间】:2018-06-14 04:27:22
【问题描述】:
我正在使用 angular-cli 和 Angular 5。我正在尝试创建一个使用另一个服务本身的服务。
但是,我不断收到“无法解析 [服务名称] 的所有参数:(?)”的交替错误。和“无法解析 e: (?, ?, ?, ?) 的所有参数。”
这是我的设置:
MyLoggerService
import { Injectable, Inject, forwardRef } from "@angular/core";
import { OtherLoggerService } from "@A/AServices";
// OtherLoggerService is from a package I installed via npm, so I won't include it here.
// I thought I was missing this @Injectable decorator, but adding it to my
//code gives me the e: (?, ?, ?, ?) error
//Without this @Injectable() line, I get the MyLoggerService: (?) error.
@Injectable()
export class DMLoggerService extends {
constructor(
@Inject(forwardRef(() => OtherLoggerService)) private otherLoggerService: OtherLoggerService
) { }
public logEvent(): void {
this.otherLoggerService.logEvent();
}
public trackPageView(): void {
this.otherLoggerService.trackPageView();
}
}
我的组件
import { Component, Inject, forwardRef} from "@angular/core";
import { OtherLoggerService} from "@A/AServices";
import { MyLoggerService } from "../../common/services/mylogger.service";
import { AnotherService } from "@A/AServices";
@Component({
selector: "my-component",
templateUrl: "./my-component.component.html"
})
export class MyComponent{
constructor(
@Inject(forwardRef(() => MyLoggerService)) private myLoggerService: MyLoggerService,
private anotherService: AnotherService
) {
this.myLoggerService.trackPageView();
this.anotherService.someFunc();
}
}
我的模块
import { NgModule } from "@angular/core";
import { BrowserModule } from "@angular/platform-browser";
import { CommonModule } from "@angular/common";
import { OtherLoggerService} from "@A/AServices"; // Just added this per comments, but still receive the same errors
import { MyLoggerService } from "../../common/services/mylogger.service";
import { AnotherService } from "@A/AServices";
@NgModule({
imports: [
CommonModule
BrowserModule
],
declarations: [],
providers: [OtherLoggerService, MyLoggerService, AnotherService]
// Added OtherLoggerService to this array but still received errors
})
export class MyModule { }
问题与某处的喷油器有关,但我不确定该去哪里。我一直在循环交替出现相同的两个错误。
据我了解,我没有使用任何桶,所以理想情况下它不应该是进口订购问题。
我看过类似的主题,但还没有找到任何可行的解决方案。
编辑:“OtherLoggerService”和“AnotherService”都是从我用 npm 安装的包中引入的服务,所以我不会在这里发布它们的内容。但是,如果我在使用它们的过程中可以学习或需要记住什么,请分享您的 cmets。
编辑:在MyModule 上添加了OtherLoggerService 的导入,但仍然收到错误。
编辑:尝试将 OtherLoggerService 添加到 MyModule 的 Providers 数组中,仍然收到错误。
【问题讨论】:
-
你能把
OtherLoggerService也加进去吗? -
@user184994 OtherLoggerService 是我没有写的包中的服务;通过 npm 安装的东西。
-
如果
OtherLoggerService来自第三方,它是否在 Angular 模块中?如果是这样,您是在 app.module 中导入该模块,还是在您编写的其他模块中导入该模块? -
@R.Richards 是的,它是来自第三方的 Angular 模块。我的代码最初看起来是如何做到的。我想,因为没有其他地方在使用
OtherLoggerService,所以我不需要导入它。我刚刚在MyModule中导入了它,但我仍然收到同样的错误。 -
这个
@A库是否包含类似@A/ServiceModule的内容? 那 是您需要导入到您编写的模块中的内容吗?如果库提供了真正的模块供您使用,您可能不需要在模块中添加@A/AServices导入。
标签: javascript angular dependency-injection angular-services