Angular 2 Provider异常

时间:2016-07-28 03:50:22

标签: angular angular2-providers

将服务注入组件时,我看到错误:

  

未定义一个或多个提供程序“CategoriesComponent”:[?,[object Object],BrowserXhr,[object Object],[object Object],XHRBackend,[object Object]]。

请帮我解决此问题。非常感谢

以下是我的代码:

category.services.ts

import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';


import { Category } from './index';
// import * as global from '../shared/global/globals';

@Injectable()
export class CategoryService {
    private _baseUrl: string = '/api/v1/category/list';

    constructor(private http: Http) {
        // this._baseUrl = global.BASE_URL;
    }

    getCategories(): Observable<Category[]>{
        return this.http.get(this._baseUrl)
            .map((res: Response) => {
                return res.json();
            })
            .catch(this.handleError);
    }

    private handleError(error: any) {
    var applicationError = error.headers.get('Application-Error');
    var serverError = error.json();
    var modelStateErrors: string = '';

    if (!serverError.type) {
      console.log(serverError);
      for (var key in serverError) {
        if (serverError[key])
          modelStateErrors += serverError[key] + '\n';
      }
    }

    modelStateErrors = modelStateErrors = '' ? null : modelStateErrors;

    return Observable.throw(applicationError || modelStateErrors || 'Server error');
  }
}

categories.component.ts

import { Component, OnInit } from '@angular/core';
import { HTTP_PROVIDERS } from '@angular/http';
import { Category, CategoryService } from './index';

@Component({
    // moduleId: module.id,
    selector: 'categories',
    templateUrl: 'views/category/categories-component',
    providers: [CategoryService, HTTP_PROVIDERS]
})
export class CategoriesComponent implements OnInit {
    categories: Category[];

    isCollapsed = false;

    constructor(private categoryService: CategoryService) {}

    ngOnInit() {
        this.categoryService.getCategories()
            .subscribe((categories: Category[]) => {
                this.categories = categories;
            }, 
            error => {
                console.log(error);
            });
        // console.log('=======');
    }

    toggle(){
        this.isCollapsed = !this.isCollapsed;
    }
}

navbar.component.ts

import { Component } from '@angular/core';
import { ROUTER_DIRECTIVES } from '@angular/router';
import { CategoriesComponent } from '../../category/index';

@Component({
    // moduleId: module.id,
    selector: 'navbar',
    templateUrl: 'views/navbar/navbar',
    directives: [ROUTER_DIRECTIVES, CategoriesComponent]
})
export class NavbarComponent {

}

1 个答案:

答案 0 :(得分:1)

您不必在您的提供者阵列中提供服务......发生的错误仅由此引起...只需在您的categories.component.ts文件中更改此内容: -

  

提供者:[CategoryService,HTTP_PROVIDERS] to ===&gt;供应商:   [HTTP_PROVIDERS]

如果您没有在app组件或main.ts文件中提供它,则可以正常工作...

您需要更改的第二件事是您的categoryservice导入路径,如下所示: -

  

从'.path导入{CategoryService}到category.service'

它会正常工作:)