Angular2:HTTP观测流量不明确

Angular2:HTTP观测流量不明确

问题描述:

在我的组件我打电话REST服务,并要保存数据,其余的呼叫不成功(预期行为),所以我期待这个代码被执行:Angular2:HTTP观测流量不明确

ERR =>执行console.log(ERR)& & this.accessLevel == AccessLevel.DISBLED

export class CustomerComponent extends SuperChildComponent{ 
    public static url:string='/orderViewCustomer/'; 
    public id; 
    public allowed: boolean = false; 
    public accessLevel:AccessLevel =null; 
    public componentname:string; 
    public customerData:Customer=null; 

    constructor(private rest:REST,private authenticationService : AuthorizationService) { 
    super(); 
    this.componentname=this.constructor.name; 
    this.accessLevel=this.authenticationService.isUserLoggedIn()?this.authenticationService.componentAccessLevel(this.constructor.name):null; 
    console.log(this.constructor.name +' has '+this.accessLevel); 
    if(this.accessLevel==AccessLevel.ENABLED){ 
     this.getData(); 
    } 
    } 

    private getData():any{ 
    this.rest.get(CustomerComponent.url,this.id).subscribe(data=> this.storeData(data.json()), err => console.log(err) && this.accessLevel==AccessLevel.DISBLED); 
    } 



private storeData(res:Object):any{ 
      //TODO 
     //this.customerData =<Customer>res; 
    this.customerData=<Customer>this.dummy; 
}  

这是我的休息服务get方法:

get(resource: string, id: number, params: URLSearchParams = new URLSearchParams()) { 
    let headers = this.defaultHeaders; 
    headers.set("Authorization", this.authdata); 
    return this 
     .http 
     .get(this.getUrl(resource) + '/' + id, params); 
    // .map((r: Response) => r.json()); 
} 

但是error block永远不会执行,storeData(dat.json())函数也不会执行。

只有在呼叫成功时,用户块才能执行吗?

有什么我在这里失踪?

感谢

+0

能否请你定义 '并不成功'?它是否返回一个错误代码或者根本不可能连接? – Sebastian

subscribe(...)支持3个回调

someObservable.subscribe(
    data => this.onSuccessDoSomething(data), 
    err => this.onErrorDoSomethingElse(err), 
() => this.afterObservableCompleted() // after the last onSuccess... 
) 
+0

重要的是要注意'onCompleted'不在错误情况下被调用,而只在'onNext'情况下被调用。 – Sebastian

+0

您是否暗示代码因为缺少参数而无法使用?如果是这样,那么我认为JavaScript会为第三个参数传递null,并且调用仍然可以工作。 – bassxzero

+0

试图回答“只有在呼叫成功后才执行用户块吗?”这个问题。 –