angular 2 rc4 http请求抛出未捕获(在promise中)异常

时间:2016-07-04 22:15:13

标签: typescript angular

升级到angular2 rc4后,用angualr 2 http服务做了什么改变?

我一直在浏览角度http客户端上的文档并整天扫描堆栈溢出,但我无法弄清楚问题。

下面是我的代码实现的片段和我得到的错误,任何人都可以告诉我我做错了什么。

这是我的代码

    import { Restaurant } from '../index'
    import { Http, Headers, RequestOptions, Response } from '@angular/http';
    import { Injectable } from '@angular/core';

    import 'rxjs/Rx';
    import { Observable } from 'rxjs/Observable';

@Injectable() 
export class RestaurantService {
    constructor(private _http: Http){}

    addRestaurant(restaurant: Restaurant): Observable<Restaurant> {
        let body = JSON.stringify({restaurant});
        let headers = new Headers({ 'Content-Type':'application/json'});
        let options = new RequestOptions({ headers: headers});
        console.log(body);
        return this._http.post('http://localhost:3000/restaurant', body, options)
            .map(this.extractData)
            .catch(this.errorHandler)
    }


    private extractData(res: Response) {
       let body = res.json();
       return body;
    }

    private errorHandler(error:any) {
       return Observable.throw(error.json());
     }
    }

并且服务在我的代码中实现

import { Component, OnInit, OnDestroy } from '@angular/core';
import { REACTIVE_FORM_DIRECTIVES, FormArray,  FORM_DIRECTIVES, FormControl, FormGroup } from '@angular/forms';
import { Restaurant } from './index';
import { RestaurantService } from '../../index';

@Component({
    moduleId: module.id,
    selector: 'sd-restaurant-form',
    templateUrl: 'restaurant-form.component.html',
    styleUrls: ['restaurant-form.component.html'],
    directives: [REACTIVE_FORM_DIRECTIVES]
})
export class RestaurantFormComponent implements OnInit, OnDestroy {
    formActive: boolean = true;


    restaurantForm = new FormGroup( {
        .........
        .........

    });

    constructor(public restaurantService: RestaurantService) { }

    onSubmit() {
        let value = this.restaurantForm.value;

        ............
        ............
        ............

        this.restaurantService.addRestaurant(this.restaurant)
        .subscribe(
            res =>{ this.result = res,
                    console.log(this.result)},
            error=> {this.error = error.
                    console.log(this.error)}
        )
    }


    ngOnInit() { 
        for (let i=0; i<24; i++) { 
            if( i === 0) {
               this.timesHour.push(0 +''+i); 
            }
            else if ((Math.log10(i)+1) < 2 ) {
                this.timesHour.push(0 +''+i);
            } else {
                this.timesHour.push(String(i));
            }
        }
        for (let i=0; i<60; i++) {
            if( i === 0) {
               this.timesMin.push(0 +''+i); 
            }
            else if ((Math.log10(i)+1) < 2 ) {
                this.timesMin.push(0 +''+i);
            } else {
                this.timesMin.push(String(i));
            }
        }
    }

    ngOnDestroy() {}

}

这是我得到的错误

EXCEPTION: Error: Uncaught (in promise): Can't resolve all parameters for RestaurantFormComponent: (?).
EXCEPTION: Error: Uncaught (in promise): Can't resolve all parameters for RestaurantFormComponent: (?).
STACKTRACE:
Error: Uncaught (in promise): Can't resolve all parameters for RestaurantFormComponent: (?).
    at resolvePromise (zone.js?1467669664341:538)
    at zone.js?1467669664341:515
    at ZoneDelegate.invoke (zone.js?1467669664341:323)
    at Object.onInvoke (core.umd.js:9100)
    at ZoneDelegate.invoke (zone.js?1467669664341:322)
    at Zone.run (zone.js?1467669664341:216)
    at zone.js?1467669664341:571
    at ZoneDelegate.invokeTask (zone.js?1467669664341:356)
    at Object.onInvokeTask (core.umd.js:9091)
    at ZoneDelegate.invokeTask (zone.js?1467669664341:355)
Unhandled Promise rejection: Can't resolve all parameters for RestaurantFormComponent: (?). ; Zone: angular ; Task: Promise.then ; Value: BaseException$1 {message: "Can't resolve all parameters for RestaurantFormComponent: (?).", stack: "Error: Can't resolve all parameters for Restaurant…ngular/compiler/bundles/compiler.umd.js:14640:81)"}
Error: Uncaught (in promise): Can't resolve all parameters for    RestaurantFormComponent: (?).(…)

1 个答案:

答案 0 :(得分:1)

即使我对angular 2 rc4也有同样的问题,导入和使用Inject解决了can't resolve all parameters的问题。这是我的代码示例:

import { Injectable,Inject } from '@angular/core';
...


@Injectable() 
export class RestaurantService {
   constructor(@Inject(Http) private _http: Http) {
...
}