当前位置:   article > 正文

Vue3+Vite+Ts的Axios企业级封装+本地存储封装_vue ts使用本地存储

vue ts使用本地存储

前言:实际项目开发中,向后台发请求,vue相关项目,都是用axios,不会用ajax,不会为了ajax单独去引入jquery.

axios官网:http://www.axios-js.com/

第一步:下载依赖axios

  1. npm i axios --save
  2. 或者
  3. cnpm i axios --save
  4. 或者
  5. yarn add axios --save

第二步:在src的目录中新建一个utils的文件夹

 第三步:request.ts中编写代码

  1. import axios, { AxiosInstance } from "axios";
  2. // 因为是ts需要定义类型
  3. import qs from "qs";
  4. class AxiosUtils {
  5. private http: AxiosInstance;
  6. constructor() {
  7. this.http = axios.create({
  8. // 根路径
  9. baseURL: "http://119.23.246.178:80",
  10. // 请求延迟时间 如果超过这个时间就会断开拦截
  11. timeout: 3000,
  12. // headers: { "X-Custom-Header": "foobar" },
  13. });
  14. // 要在constructor里面进行调用 发请求的时候就要开始调用 就要对请求和响应进行拦截
  15. this.myAddinterceptors();
  16. }
  17. // 拦截器
  18. // 拦截器要自己定义一个方法.实现拦截
  19. private myAddinterceptors() {
  20. //封装的是拦截器
  21. //请求拦截器
  22. // 一般的作用是 拦截token或者请求头
  23. // 添加请求拦截器
  24. this.http.interceptors.request.use(
  25. function (config) {
  26. // 在发送请求之前做些什么
  27. return config;
  28. },
  29. function (error) {
  30. // 对请求错误做些什么
  31. return Promise.reject(error);
  32. }
  33. );
  34. // 添加响应拦截器
  35. this.http.interceptors.response.use(
  36. function (response) {
  37. // 2xx 范围内的状态码都会触发该函数。
  38. // 对响应数据做点什么
  39. //对返回的数据进行筛选
  40. //response.data 只取data中的值,其他的属性不要
  41. return response.data;
  42. },
  43. function (error) {
  44. // 超出 2xx 范围的状态码都会触发该函数。
  45. // 对响应错误做点什么
  46. return Promise.reject(error);
  47. }
  48. );
  49. }
  50. // 封装一个request方法
  51. private request(url: string, method: string, data: any = {}) {
  52. return this.http({
  53. url,
  54. method,
  55. params: method == "get" ? data : "",
  56. data: method == "post" ? qs.stringify(data) : "",
  57. });
  58. }
  59. // public公开的,意思就是让别人用 private自己封装的 需要隐藏起来 不让别人用
  60. // 封装get方法
  61. public get(url: string, data: any) {
  62. return this.request(url, "get", data);
  63. }
  64. // 封装post方法
  65. public post(url: string, data: any) {
  66. return this.request(url, "post", data);
  67. }
  68. }
  69. // 新建对象
  70. let http = new AxiosUtils();
  71. // 把对象暴露出去
  72. export default http;

理解为什么在constructor中进行调用:

 第四步:在src目录下新建api文件夹,创建模块ts文件

 在模块文件中编辑代码片段:

  1. import request from "../utils/request";
  2. export const api_login = (data: any) => {
  3. return request.post("/users/checkLogin", data);
  4. };

本地存储localStorage和sessionStorage的方法封装

  1. //定义本地存储的接口类
  2. export interface LocalInterFace {
  3. // 获取
  4. get<T>(key: string): T;
  5. // 设置
  6. set<T>(key: string, value: T): void;
  7. // 移除
  8. remove(key: string): void;
  9. // 判断是否存在
  10. isKey(key: string): boolean;
  11. // 清除
  12. clear(): void;
  13. }
  14. // 定义localStorage封装
  15. class LocalUtil implements LocalInterFace {
  16. // 获取
  17. get<T>(key: string): T | any {
  18. // ts类型推断时不能将null赋值给JSON.parse()的参数
  19. let str = window.localStorage.getItem(key) || "";
  20. if (str) {
  21. return JSON.parse(str);
  22. }
  23. return str;
  24. }
  25. // 设置
  26. set<T>(key: string, value: T): void {
  27. window.localStorage.setItem(key, JSON.stringify(value));
  28. }
  29. // 移除
  30. remove(key: string): void {
  31. window.localStorage.removeItem(key);
  32. }
  33. // 判断是否存在
  34. isKey(key: string): boolean {
  35. let str = window.localStorage.getItem(key);
  36. return str ? true : false;
  37. }
  38. // 清除
  39. clear(): void {
  40. window.localStorage.clear();
  41. }
  42. }
  43. // 定义sessionStorage封装
  44. class SessionUtil implements LocalInterFace {
  45. // 获取
  46. get<T>(key: string): T | any {
  47. // ts类型推断时不能将null赋值给JSON.parse()的参数
  48. let str = window.sessionStorage.getItem(key) || "";
  49. if (str) {
  50. return JSON.parse(str);
  51. }
  52. return str;
  53. }
  54. // 设置
  55. set<T>(key: string, value: T): void {
  56. window.sessionStorage.setItem(key, JSON.stringify(value));
  57. }
  58. // 移除
  59. remove(key: string): void {
  60. window.sessionStorage.removeItem(key);
  61. }
  62. // 判断是否存在
  63. isKey(key: string): boolean {
  64. let str = window.sessionStorage.getItem(key);
  65. return str ? true : false;
  66. }
  67. // 清除
  68. clear(): void {
  69. window.sessionStorage.clear();
  70. }
  71. }
  72. // 创建需要导出的对象
  73. const $local = new LocalUtil();
  74. const $session = new SessionUtil();
  75. // 以对象的形式导出
  76. export { $local, $session };

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/227607
推荐阅读
相关标签
  

闽ICP备14008679号