【问题标题】:Angular2 EXCEPTION: No provider for e! (e -> e)Angular2 例外:没有 e 的提供者! (e -> e)
【发布时间】:2016-04-18 03:46:24
【问题描述】:

所以我有一个非常简单的组件,它加载了一个简单的路由器。我正在使用所有基本的东西,比如 ngFor、ngSwitch、ngIf,我通过 COMMON_DIRECTIVES 注入它们

我得到这个非常模糊的错误,没有堆栈跟踪,所以我真的不知道发生了什么。我在https://github.com/angular/angular/issues/6223 中看到了一些可能类似的东西。

例外:没有 e 的提供者! (e -> e)

堆栈跟踪:

错误:DI 异常 at t [作为构造函数] (http://localhost:5000/node_modules/angular2/bundles/angular2.min.js:6:5082) at t [作为构造函数] (http://localhost:5000/node_modules/angular2/bundles/angular2.min.js:1:26551) 在新 t (http://localhost:5000/node_modules/angular2/bundles/angular2.min.js:1:27079) 在 e._throwOrNull (http://localhost:5000/node_modules/angular2/bundles/angular2.min.js:9:5943) 在 e._getPrivateDependency (http://localhost:5000/node_modules/angular2/bundles/angular2.min.js:9:6605) 在 e._getByKeyHost (http://localhost:5000/node_modules/angular2/bundles/angular2.min.js:9:6397) 在 e._getByKey (http://localhost:5000/node_modules/angular2/bundles/angular2.min.js:9:5826) 在 e._getByDependency (http://localhost:5000/node_modules/angular2/bundles/angular2.min.js:9:5602) 在 e._instantiate (http://localhost:5000/node_modules/angular2/bundles/angular2.min.js:9:3644) 在 e._instantiateProvider (http://localhost:5000/node_modules/angular2/bundles/angular2.min.js:9:3376)

这是我的代码:

.玉:

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

【问题讨论】:

  • 不需要添加COMMON_DIRECTIVESFORM_DIRECTIVES,它们是自动添加的。所以最好删除它们。您是否将 HTTP_PROVIDERS 添加到您的 bootstrap 函数中?
  • 你的意思是它们是自动添加的?有这个来源吗?还要检查我的编辑
  • 我在更新日志中找不到它,但也不需要在引导程序中添加FORM_PROVIDERS。但正如 thierry 所说,尝试使用 .dev.js 版本来获得更详细的错误描述

标签: angular


【解决方案1】:

要获得更多可读错误,您可以使用 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 的建议。我已经将其引导...(请参阅我的编辑)
  • 看来我不见了> 例外:没有 NgSwitch 的提供者! (NgSwitchWhen -> NgSwitch) 但是我添加它之后的事件仍然给我错误
  • 你是否在你的组件上定义了这个:directives: [NgSwitch, NgSwitchWhen, NgSwitchDefault]。这是一个使用示例:plnkr.co/edit/DQMTII95CbuqWrl3lYAs?p=preview.
  • 非常感谢。那是我在包含 ngSwitch 之外拥有第二个 ngSwitchWhen
  • 如果您提到开关错误并且可能会加粗或添加 .dev 确实会有所帮助,因为它确实有帮助,我会接受您的回答
猜你喜欢
  • 2020-10-26
  • 1970-01-01
  • 2015-06-18
  • 2016-01-11
  • 1970-01-01
  • 1970-01-01
  • 2017-10-09
  • 2015-05-19
  • 1970-01-01
相关资源
最近更新 更多