分享更有价值
被信任是一种快乐

Angular怎么实现错误处理和请求拦截

文章页正文上

本篇内容主要讲解“Angular怎么实现错误处理和请求拦截”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Angular怎么实现错误处理和请求拦截”吧!用 Angular 提供的 HttpClient 可以很轻松的实现 API 接口的访问。举个例子 新建一个 http.service.ts 可以在 environment 中配置不同环境的 host 地址再贴一下 proxy.config.json中有介绍到

{
"/api":{
"target":"http://124.223.71.181",
"secure":true,
"logLevel":"debug",
"changeOrigin":true,
"headers":{
"Origin":"http://124.223.71.181"
}
}
}

import{HttpClient}from'@angular/common/http';
import{Injectable}from'@angular/core';
import{environment}from'@env';

@Injectable({providedIn:'root'})
exportclassHttpService{
constructor(privatehttp:HttpClient){}

publicechoCode(method:'get'|'post'|'delete'|'put'|'patch'='get',params:{code:number}){
switch(method){
case'get':
case'delete':
returnthis.http[method](`${environment.backend}/echo-code`,{params});
case'patch':
case'put':
case'post':
returnthis.http[method](`${environment.backend}/echo-code`,params);
}
}
}

然后在业务中 我们就可以这样使用

import{Component,OnInit}from'@angular/core';
import{HttpService}from'./http.service';

@Component({
selector:'http',
standalone:true,
templateUrl:'./http.component.html',
})
exportclassHttpComponentimplementsOnInit{
constructor(privatehttp:HttpService){}
ngOnInit():void{
this.http.echoCode('get',{code:200}).subscribe(console.log);
this.http.echoCode('post',{code:200}).subscribe(console.log);
this.http.echoCode('delete',{code:301}).subscribe(console.log);
this.http.echoCode('put',{code:403}).subscribe(console.log);
this.http.echoCode('patch',{code:500}).subscribe(console.log);
}
}

这看起来非常简单 类似 Axios下面介绍一下一些常用的用法

this.http
.echoCode('get',{code:200})
.pipe(catchError((err:HttpErrorResponse)=>of(err)))
.subscribe((x)=>{
if(xinstanceofHttpErrorResponse){
//dosomething
}else{
//dosomething
}
});

请求拦截是比较常用的例如 你可以在这里判断 cookie 是否有效 / 全局错误处理 …新建 http-interceptor.ts 文件 ( 文件名可以随意 )最主要的是要实现 HttpInterceptorintercept 方法

import{HttpInterceptor,HttpRequest,HttpHandler,HttpResponse,HttpErrorResponse}from'@angular/common/http';
import{Injectable}from'@angular/core';
import{Observable,of,throwError}from'rxjs';
import{filter,catchError}from'rxjs/operators';
import{HttpEvent}from'@angular/common/http';

@Injectable()
exportclassHttpInterceptorServiceimplementsHttpInterceptor{
constructor(){}
intercept(req:HttpRequest,next:HttpHandler):Observable>{
returnnext
.handle(req)
.pipe(filter((event)=>eventinstanceofHttpResponse))
.pipe(
catchError((error)=>{
console.log('catcherror',error);
returnof(error);
})
);
}
}

然后在 module 中的 providers 中使用 这个拦截器就生效了

@NgModule({
imports:[RouterModule.forChild(routes)],
exports:[RouterModule],
providers:[
{
provide:HTTP_INTERCEPTORS,
useClass:HttpInterceptorService,
multi:true,
},
],
})
免费云主机、域名
exportclassXXXModule{}

到此,相信大家对“Angular怎么实现错误处理和请求拦截”有了更深的了解,不妨来实际操作一番吧!这里是云技术网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

相关推荐: 怎么使用Jquery插件实现跨域异步上传文件功能

这篇文章主要介绍“怎么使用Jquery插件实现跨域异步上传文件功能”,在日常操作中,相信很多人在怎么使用Jquery插件实现跨域异步上传文件功能问题上存在疑惑,小编查阅了各式资料,免费云主机、域名整理出简单好用的操作方法,希望对大家解答”怎么使用Jquery插…

文章页内容下
赞(0) 打赏
版权声明:本站采用知识共享、学习交流,不允许用于商业用途;文章由发布者自行承担一切责任,与本站无关。
文章页正文下
文章页评论上

云服务器、web空间可免费试用

宝塔面板主机、支持php,mysql等,SSL部署;安全高速企业专供99.999%稳定,另有高防主机、不限制内容等类型,具体可咨询QQ:360163164,Tel同微信:18905205712

主机选购导航云服务器试用

登录

找回密码

注册