Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rxjs 如何观察某些部件的角5拦截器误差_Rxjs_Angular5_Angular Http Interceptors - Fatal编程技术网

Rxjs 如何观察某些部件的角5拦截器误差

Rxjs 如何观察某些部件的角5拦截器误差,rxjs,angular5,angular-http-interceptors,Rxjs,Angular5,Angular Http Interceptors,嗨,我是angular 5的新手,在一些博客上写了HTTP拦截器 export class AngularInterceptor implements HttpInterceptor { public http404 = false; constructor() { } intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { console.

嗨,我是angular 5的新手,在一些博客上写了HTTP拦截器

export class AngularInterceptor implements HttpInterceptor {
public http404 = false;
constructor() { }

intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {

    console.log("intercepted request ... ");

    // Clone the request to add the new header.
    const httpReq = req.clone(
        {
            headers: req.headers.set("headerName", "headerValue")
        }
    );

    console.log("Sending request with new header now ...");

    //send the newly created request
    return next.handle(httpReq)
        .catch((error, caught) => {
            //intercept the respons error and displace it to the console 
            console.log("Error Occurred");
            if(error.status === 404)
this.http404 = true;
//need to pass this value to another component. Let's say app.component.ts and display some message to the user.
            //return the error to the method that called it
            return Observable.throw(error);
        }) as any;
}
导出类AngularInterceptor实现HttpInterceptor{
公共http404=假;
构造函数(){}
截取(req:HttpRequest,next:HttpHandler):可观察{
log(“截获的请求…”);
//克隆添加新标头的请求。
const httpReq=req.clone(
{
headers:req.headers.set(“headerName”、“headerValue”)
}
);
log(“正在发送带有新头的请求…”);
//发送新创建的请求
返回next.handle(httpReq)
.catch((错误,捕获)=>{
//截取响应错误并将其转移到控制台
console.log(“发生错误”);
如果(error.status==404)
this.http404=true;
//需要将此值传递给另一个组件。例如app.component.ts,并向用户显示一些消息。
//将错误返回给调用它的方法
返回可观察抛出(错误);
})如有的话;
}
}

这很好用。但我需要做的是将此错误代码传递给其他组件,并在屏幕上为用户打印一条消息。要做到这一点,一个wy就是创建一个可观察的对象,但我无法实现它


非常感谢您提供的任何帮助。

您可以利用
主题使用服务来实现这一点。下面是一个使用
行为主题
的示例

首先创建一个服务。此服务将在两个类之间共享:

导出类广播服务{
公共http404:行为主体;
构造函数(){
//将其初始化为false
this.http404=新行为主体(false);
}
}
在您的
HttpInterceptor
类中,您将
BroadcastService
注入其中。要更新
BehvaiourSubject
,只需使用
.next()

导出类AngularInterceptor实现HttpInterceptor{
公共http404=假;
构造器(公共广播服务:广播服务){
}
截取(req:HttpRequest,next:HttpHandler):可观察{
log(“截获的请求…”);
//克隆添加新标头的请求。
const httpReq=req.clone({
headers:req.headers.set(“headerName”、“headerValue”)
});
log(“正在发送带有新头的请求…”);
//发送新创建的请求
返回next.handle(httpReq)
.catch((错误,捕获)=>{
//截取响应错误并将其转移到控制台
console.log(“发生错误”);
如果(error.status==404)
this.http404=true;
//需要将此值传递给另一个组件。例如app.component.ts,并向用户显示一些消息。
this.broadcastService.http404.next(true);
//将错误返回给调用它的方法
返回可观察抛出(错误);
})如有的话;
}
}
在您的
app.component.ts
中,只需使用
.asObservable()
订阅即可。您也需要注入它:

export类AppComponent实现ngOnInit{
构造器(公共广播服务:广播服务){
}
OnInit(){
this.broadCastService.http404.asObservable().subscribe(值=>{
console.log(值);//如果http错误,将返回false
});
}
}