首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >角2提供程序异常

角2提供程序异常
EN

Stack Overflow用户
提问于 2016-07-28 03:50:22
回答 1查看 502关注 0票数 0

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

未定义一个或多个提供者"CategoriesComponent“:[?、对象对象、BrowserXhr、对象、XHRBackend、对象]。

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

下面是我的代码:

category.services.ts

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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 {

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-07-28 04:51:17

您不必在供应商数组中提供服务……发生的错误是由于此only...just更改了categories.component.ts文件中的以下内容:-

提供者: CategoryService,HTTP_PROVIDERS to===> providers: HTTP_PROVIDERS

如果您没有在应用程序组件或main.ts文件中提供它,则此操作有效.

您需要更改的第二件事是类别服务导入的路径,如下所示:

将{ CategoryService }从“.path导入到category.service”

它将很好地工作:)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38626788

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档