加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angular2 – 角度2 DI错误 – EXCEPTION:无法解析所有参数

发布时间:2020-12-17 08:59:42 所属栏目:安全 来源:网络整理
导读:我在Angular 2中构建了一个基本的应用程序,并遇到一个奇怪的问题,我无法注入一个服务到我的组件之一。它注入精细到任何我创建的三个其他组件。 对于初学者这是服务(它是非常基本的,因为我只是测试的东西现在): import { Injectable } from '@angular/cor
我在Angular 2中构建了一个基本的应用程序,并遇到一个奇怪的问题,我无法注入一个服务到我的组件之一。它注入精细到任何我创建的三个其他组件。

对于初学者这是服务(它是非常基本的,因为我只是测试的东西现在):

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:(?)的所有参数。

我有在引导功能的服务,所以它有一个提供程序。我似乎能够将它注入我的任何其他组件的构造函数没有问题。

有没有人看到任何明显的原因,为什么这不工作?

从直接声明它而不是桶的文件中导入它。

我不知道究竟是什么原因导致的问题,但我看到它提到几次(可能是某种循环依赖)。

它也应该通过改变出口在桶中的顺序是固定的(不知道细节,但也提到)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读