例外:无法解析所有参数 [英] EXCEPTION: Can't resolve all parameters

查看:136
本文介绍了例外:无法解析所有参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've built a basic app in Angular 2, but I have encountered a strange issue where I cannot inject a service into one of my components. It injects fine into any of the three other components I have created, however.

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

For starters, this is the service:

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;
  }

}

以及它拒绝使用的组件:

And the component that it refuses to work with:

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);
  }

}

我在浏览器控制台中遇到的错误是:

The error I get in the browser console is this:

例外:无法解析HeaderComponent的所有参数:(?).

EXCEPTION: Can't resolve all parameters for HeaderComponent: (?).

我在bootstrap函数中具有该服务,因此它具有提供程序.而且,我似乎可以毫无问题地将其注入到其他任何组件的构造函数中.

I have the service in the bootstrap function so it has a provider. And I seem to be able to inject it into the constructor of any of my other components without issue.

推荐答案

错误#1:忘记装饰器:

//Uncaught Error: Can't resolve all parameters for MyFooService: (?).
export class MyFooService { ... }

错误#2:省略"@"符号:

//Uncaught Error: Can't resolve all parameters for MyFooService: (?).
Injectable()
export class MyFooService { ... }

错误#3:省略()"符号:

//Uncaught Error: Can't resolve all parameters for TypeDecorator: (?).
@Injectable
export class MyFooService { ... }

错误#4:小写的"i":

//Uncaught ReferenceError: injectable is not defined
@injectable
export class MyFooService { ... }

错误#5:您忘记了: 从"@ angular/core"导入{可注射};

//Uncaught ReferenceError: Injectable is not defined
@Injectable
export class MyFooService { ... }

正确:

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

这篇关于例外:无法解析所有参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆