我已经用Angular构建了一个基本的应用程序,但是我遇到了一个奇怪的问题,我不能将服务注入到我的一个组件中。但是,它可以很好地注入我创建的其他三个组件。

对于初学者来说,这是服务:

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

@Injectable()
export class MobileService {
  screenWidth: number;
  screenHeight: number;

  constructor() {
    this.screenWidth = window.outerWidth;
    this.screenHeight = window.outerHeight;

    window.addEventListener("resize", this.onWindowResize.bind(this) )
  }
  
  onWindowResize(ev: Event) {
    var win = (ev.currentTarget as Window);
    this.screenWidth = win.outerWidth;
    this.screenHeight = win.outerHeight;
  }
  
}

以及它拒绝使用的组件:

import { Component, } from '@angular/core';
import { NgClass } from '@angular/common';
import { ROUTER_DIRECTIVES } from '@angular/router';

import {MobileService} from '../';

@Component({
  moduleId: module.id,
  selector: 'pm-header',
  templateUrl: 'header.component.html',
  styleUrls: ['header.component.css'],
  directives: [ROUTER_DIRECTIVES, NgClass],
})
export class HeaderComponent {
  mobileNav: boolean = false;

  constructor(public ms: MobileService) {
    console.log(ms);
  }

}

我在浏览器控制台得到的错误是这样的:

EXCEPTION:不能解析HeaderComponent:(?)的所有参数。

我在bootstrap函数中有服务,所以它有一个提供者。而且我似乎能够将它注入到任何其他组件的构造函数中而没有任何问题。


当前回答

通过将服务A注入到服务B中,我也遇到了这种情况,反之亦然。

我认为这种快速失败是件好事,因为无论如何都应该避免。如果您希望您的服务更加模块化和可重用,最好尽可能避免循环引用。这篇文章强调了围绕这一点的陷阱。

因此,我有以下建议:

如果您觉得类之间的交互太频繁(我说的是特性嫉妒),您可能会考虑将这两个服务合并到一个类中。 如果上面的方法对你不起作用,考虑使用第三个服务(EventService),两个服务都可以注入它来交换消息。

其他回答

如前所述,这个问题是由桶内的导出排序引起的,而桶内的导出排序是由循环依赖关系引起的。

更详细的解释在这里:https://stackoverflow.com/a/37907696/893630

如果您的服务与使用它的组件定义在同一个文件中,并且服务定义在文件中的组件之后,您可能会得到此错误。这是由于其他人提到的相同的“forward dref”问题。此时,VSCode并不能很好地向您显示这个错误,并且构建编译成功。

由于编译器的工作方式(可能与树摇晃有关),使用——aot运行构建可以掩盖这个问题。

解决方案:确保在另一个文件中或在组件定义之前定义服务。(我不确定在这种情况下是否可以使用forwardRef,但这样做似乎很笨拙)。

如果我有一个非常简单的服务,它与组件紧密地绑定在一起(有点像视图模型)。ImageCarouselComponent,我可以把它命名为ImageCarouselComponent。service。ts这样它就不会和其他服务混在一起。

添加Injectable解决了我的问题:

错误代码

import { Observable } from 'rxjs';
import { io } from 'socket.io-client';
import { HttpClient } from '@angular/common/http';

export class Chat {

修复后

import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { io } from 'socket.io-client';
import { HttpClient } from '@angular/common/http';

@Injectable()
export class Chat {

在我的例子中,我错过了调用我继承的类的构造函数。

之前:

@Component({ ... })
export class ComponentA extends ComponentParent {
  // ...
}

后:

@Component({ ... })
export class ComponentA extends ComponentParent {
  constructor(protected route: ActivatedRoute, protected store$: Store<AppState>) {
    super(route, store$);
  }
  // ...
}

虽然已经提到了从桶中导出类的排序,但下面的场景也可能产生相同的效果。

假设你有类A, B和C从同一个文件中导出,其中A依赖于B和C:

@Injectable()
export class A {
    /** dependencies injected */
    constructor(private b: B, private c: C) {}
}

@Injectable()
export class B {...}

@Injectable()
export class C {...}

因为Angular还不知道依赖类(比如类B和C),(可能是在运行时Angular对类A的依赖注入过程中)就会引发这个错误。

解决方案

解决方案是在执行DI的类之前声明和导出依赖类。

例如,在上面的例子中,类A是在它的依赖关系定义之后声明的:

@Injectable()
export class B {...}

@Injectable()
export class C {...}

@Injectable()
export class A {
    /** dependencies injected */
    constructor(private b: B, private c: C) {}
}