我已经用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函数中有服务,所以它有一个提供者。而且我似乎能够将它注入到任何其他组件的构造函数中而没有任何问题。


当前回答

我在输入错误的服务名称时遇到了这个错误,即构造函数(私有myService: myService)。

对于拼写错误的服务,我能够通过检查Chrome->控制台中的页面来确定哪个服务是问题(我在构造函数中列出了几个)。您将看到作为消息的一部分的“参数”数组列表显示对象对象,对象对象,?(或者类似的事情)。请注意“?”在哪里,这是引起问题的服务的位置。

其他回答

另一种可能是在tsconfig.json中没有将emitDecoratorMetadata设置为true

{
  "compilerOptions": {

     ...

    "emitDecoratorMetadata": true,

     ...

    }

}

在我的例子中,我从同一个组件文件中导出了一个类和一个Enum:

mComponent.component.ts:

export class MyComponentClass{...}
export enum MyEnum{...}

然后,我试图从MyComponentClass的子类中使用MyEnum。这导致了“不能解决所有参数”错误。

通过将MyEnum从MyComponentClass移动到一个单独的文件夹中,这解决了我的问题!

正如Günter Zöchbauer提到的,这是因为服务或组件是循环依赖的。

明白了!

如果以上答案都对您没有帮助,那么您可能正在从组件注入服务的同一个文件中导入某个元素。

我解释得更好:

这是服务文件:

// your-service-file.ts
import { helloWorld } from 'your-component-file.ts'

@Injectable()
export class CustomService() {
  helloWorld()
}

这是组件文件:

@Component({..})
export class CustomComponent {
  constructor(service: CustomService) { }
}

export function helloWorld() {
  console.log('hello world');
}

因此,即使符号不在同一个组件中,而只是在同一个文件中,也会引起问题。将符号(可以是函数、常量、类等等)移动到其他地方,错误就会消失

在我的情况下,修复是替换相对路径与绝对 之前

import {Service} from './service';

import {Service} from 'src/app/services/service';

看起来是typescript/angular的问题

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

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

因此,我有以下建议:

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