Angular2异常:没有提供程序e!(e-> e)

4
所以我有一个非常简单的组件,通过一个简单的路由器进行加载。我使用了所有基本的东西,如ngFor、ngSwitch、ngIf,并通过COMMON_DIRECTIVES注入它们。
我得到了一个非常模糊的错误,没有堆栈跟踪,所以我真的不知道发生了什么。我在https://github.com/angular/angular/issues/6223中看到了一些可能类似的东西。

异常:没有提供者 e! (e -> e)

堆栈跟踪:

错误:DI 异常 at t [as constructor] (http://localhost:5000/node_modules/angular2/bundles/angular2.min.js:6:5082) at t [as constructor] (http://localhost:5000/node_modules/angular2/bundles/angular2.min.js:1:26551) at new t (http://localhost:5000/node_modules/angular2/bundles/angular2.min.js:1:27079) at e._throwOrNull (http://localhost:5000/node_modules/angular2/bundles/angular2.min.js:9:5943) at e._getPrivateDependency (http://localhost:5000/node_modules/angular2/bundles/angular2.min.js:9:6605) at e._getByKeyHost (http://localhost:5000/node_modules/angular2/bundles/angular2.min.js:9:6397) at e._getByKey (http://localhost:5000/node_modules/angular2/bundles/angular2.min.js:9:5826) at e._getByDependency (http://localhost:5000/node_modules/angular2/bundles/angular2.min.js:9:5602) at e._instantiate (http://localhost:5000/node_modules/angular2/bundles/angular2.min.js:9:3644) at e._instantiateProvider (http://localhost:5000/node_modules/angular2/bundles/angular2.min.js:9:3376)

这是我的代码:

.jade:

.center-area('data-editable'="true")
  header.center-header
    .view-path Upravljaj administratorima
  .center-content
    header.content-header
      .content-header-left
        .content-header-title Administratori
        .content-header-subtitle
          | Pregledajte i upravljajte administratorima
          | radiopostaje – dodijelite administrativne
          | ovlasti korisnicima ili ih uklonite postojećim administratorima. 
          b Sustav smije imati najviše 10 administratora.
      .content-header-right
        i.material-icons.icon edit
    nav.content-options( '[ngSwitch]'="editable" '[ngClass]'="{'uneditable-ui': !editable, 'editable-ui': editable}" )
      button( '*ngSwitchWhen'="false" '(click)'="toggleEditable()" ) Uredi popis administratora
    nav.content-options.editable-ui
      button( '*ngSwitchWhen'="true" '(click)'="toggleEditable()" ) Završi uređivanje popisa
    ul.content-primary.content-list
      li.content-list-item.admin-item( '*ngFor'="#admin of admins")
        .content-list-item-content
          .content-list-item-name {{admin.first_name}} {{admin.last_name}}
          .content-list-item-data
            .content-list-item-data-item.user-mail {{admin.email}}
            .content-list-item-data-item.user-age {{admin.year_of_birth}}
            .content-list-item-data-item.user-occupation {{admin.occupation}}
        .content-list-item-options.editable-ui
          button.alt
            i.material-icons clear
      li.content-list-item.content-list-search-item.editable-ui( '*ngIf'="editable" )
        form
          .content-list-item-content
            label(for='add-item-search') Dodaj administratora
            input.add-item-search(type='text', name='add_track_search')
          .content-list-item-options
            button.raised.add-item-button
              i.material-icons person_add

.ts:

import { View, Component } from 'angular2/core';
import { Location, RouteConfig, RouterLink, Router, CanActivate } from 'angular2/router';
import { Http } from 'angular2/http';
import { COMMON_DIRECTIVES, FORM_DIRECTIVES, FormBuilder, ControlGroup, Validators, Control } from 'angular2/common';

import { Form } from '../../utilities';

@Component({
    selector: 'ManageAdmins',
    templateUrl: './dest/settings/manageAdmins/manageAdmins.html',
    directives: [COMMON_DIRECTIVES, FORM_DIRECTIVES]
})
export class ManageAdmins {
    admins: Admin[];

    editable: boolean;

    toggleEditable() {
        this.editable = !this.editable;
    }

    constructor(http: Http) {
        this.editable = false;
        this.admins = new Array();
        http.get('/owner/admins/list').map((res) => res.json().data).subscribe((res) => {
            for (let i in res) {
                let admin = new Admin(res[i]);
                this.admins.push(admin);
            }
        });
    }
}

class Admin {
    id: number;
    first_name: string;
    last_name: string;
    email: string;
    year_of_birth: string;
    occupation: string;

    constructor(obj: Object) {
        this.id = obj['id'];
        this.first_name = obj['first_name'];
        this.last_name = obj['last_name'];
        this.email = obj['email'];
        this.year_of_birth = obj['year_of_birth'];
        this.occupation = obj['occupation'];
    }
}

编辑: 我的根.ts文件

import { FORM_PROVIDERS } from 'angular2/common';
import { ROUTER_PROVIDERS, Location, LocationStrategy, PathLocationStrategy } from 'angular2/router';
import { HTTP_PROVIDERS, RequestOptions, BaseRequestOptions } from 'angular2/http';
import { bootstrap } from 'angular2/platform/browser';
import { provide, Injectable } from 'angular2/core';

import { App } from './app/app';

@Injectable()
export class DefaultRequestOptions extends BaseRequestOptions {
    constructor() {
        super();
        this.headers.set("Content-Type", "application/x-www-form-urlencoded");
    }
}

bootstrap(
    App,
    [
        FORM_PROVIDERS,
        ROUTER_PROVIDERS,
        HTTP_PROVIDERS,
        provide(LocationStrategy, { useClass: PathLocationStrategy }),
        provide(RequestOptions, { useClass: DefaultRequestOptions })
    ]
);

2
不需要添加 COMMON_DIRECTIVESFORM_DIRECTIVES,它们会自动添加。所以最好将它们删除。你是否在 bootstrap 函数中添加了 HTTP_PROVIDERS - Poul Kruijt
你是说它们会自动添加吗?有什么来源吗?还请检查我的编辑。 - ditoslav
我在更新日志中找不到,但是将 FORM_PROVIDERS 添加到引导程序中也是不必要的。但正如 Thierry 所述,尝试使用 .dev.js 版本以获取更详细的错误描述。 - Poul Kruijt
1个回答

8
为了获得更易读的错误信息,你可以使用 Angular2 分发中的 xxx.dev.js 文件。
你的错误信息说在尝试注入某些内容时出现了问题。根据你提供的代码,这可能与注入 Http 类的实例有关。你是否在引导应用程序时添加了 HTTP_PROVIDERS
import {bootstrap} from 'angular2/platform/browser';
import {AppComponent} from './app.component';
import {HTTP_PROVIDERS} from 'angular2/http';

bootstrap(AppComponent, [ HTTP_PROVIDERS ]);

感谢.dev的建议。不过我已经引导它了...(请参见我的编辑) - ditoslav
似乎我缺少“NgSwitch”的提供程序!(NgSwitchWhen-> NgSwitch),但即使我添加了它,仍然会出现错误。 - ditoslav
你在组件中定义了这个吗:directives: [NgSwitch, NgSwitchWhen, NgSwitchDefault]。这里有一个使用示例:http://plnkr.co/edit/DQMTII95CbuqWrl3lYAs?p=preview。 - Thierry Templier
非常感谢您。问题在于我将第二个ngSwitchWhen放在了包含ngSwitch的外部。 - ditoslav
如果您提到了开关错误,并且可能会加粗或其他方式突出显示添加 .dev 确实有帮助的事实,我将接受您的答案。 - ditoslav
我知道这个问题已经很久了,所以也许这些建议已经不再相关,但是你说的“使用 Angular2 发布中的 xxx.dev.js 文件”是什么意思呢? 我也遇到了一个超级难懂的 Nullinjector 错误,完全不知道从哪里开始解决。 - phelhe

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接