Angular DI錯誤 - EXCEPTION:無法解析所有參數(shù)我在Angular中構(gòu)建了一個基本的應(yīng)用程序,但是我遇到了一個奇怪的問題,我不能將服務(wù)注入到我的一個組件中。它注入了我創(chuàng)建的其他三個組件中的任何一個。首先,這是服務(wù):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的所有參數(shù):(?)。我在bootstrap函數(shù)中有服務(wù),所以它有一個提供程序。我似乎能夠毫無問題地將它注入我的任何其他組件的構(gòu)造函數(shù)中。
3 回答

繁星點(diǎn)點(diǎn)滴滴
TA貢獻(xiàn)1803條經(jīng)驗 獲得超3個贊
從直接聲明它的文件中導(dǎo)入它而不是桶。
我不知道究竟是什么導(dǎo)致了這個問題,但我看到它多次提到(可能是某種循環(huán)依賴)。
它也應(yīng)該通過改變桶中出口的順序來修復(fù)(不知道細(xì)節(jié),但也被提到)

呼如林
TA貢獻(xiàn)1798條經(jīng)驗 獲得超3個贊
除了之前給出的答案之外,當(dāng)您的注射服務(wù)缺少實際@Injectable()
裝飾器時,似乎也會拋出此錯誤。因此,在調(diào)試循環(huán)依賴項和導(dǎo)入/導(dǎo)出的順序之前,請檢查您的服務(wù)是否實際已@Injectable()
定義。
這適用于當(dāng)前的Angular最新版Angular 2.1.0。
- 3 回答
- 0 關(guān)注
- 636 瀏覽
添加回答
舉報
0/150
提交
取消