vue 表单验证,具体的大家根据自身项目和框架来,本文以antd举例。
 
axios 和 api 的封装一般选择在pages同级的根目录下,创建utils文件夹,utils文件夹下创建axios.ts
import  axios, { AxiosResponse , AxiosRequestConfig  } from  'axios' ;import  router from  "@/router" ;import  { pinia, notification } from  "@/utils/utils" ; import  { storage } from  '@/utils/storage' ; import  userStore from  '@/store/user' const  useUserStore = userStore (pinia)let  baseURL : string = '' if  (process.env .NODE_ENV  == "development" ) {    baseURL = 'http://development.com/api/'  } else  {     baseURL = 'http://pro.com/api/'  } console .log (process.env .NODE_ENV , 'node-env' )let  config = {    baseURL : baseURL,     timeout : 30000    } const  service = axios.create (config);service.interceptors .request .use (     (config: AxiosRequestConfig ) =>  {                           config.headers  = config.headers  || {}         config.headers .token  = storage.get ('token' ) || ''          return  config;     },     (error: any ) =>  {         Promise .reject (error);     } ); service.interceptors .response .use (     async  (res : AxiosResponse ) => {         const  code : number = res.data .code                   if  (code === 1001 ) {             notification ('错误提示' , res.data .msg , 'error' )                                       router.push ({path : '/login' })             return  Promise .reject (res.data )         }         return  res.data      },     (error: any ) =>  {                  return  Promise .reject (error);     } ); export  default  service;
 
在pages同级的根目录下,创建api文件夹,api文件夹下创建index.ts
import  request from  '@/utils/axios' ;export  const  getList  = (params: Object  ) => {    return  request ({         url : '/getList' ,         method : 'get' ,         params : params     }); }; export  const  setData  = (params: Object  = {} ) => {    return  request ({         url : '/setData' ,         method : 'post' ,         params : params     }); }; 
 
使用: 
import  { getData } from  '@/api/index' ; import  { loading, notification } from  "@/utils/utils" ; const  onGetData  = ( ) => {  loading ()    getData ({     id : 1    }).then (res  =>  {     loading ().close ()      if  (res.code  === 1 ) {       notification ('成功提示' , res.msg , 'success' )            }   }).catch (e  =>  {     notification ('错误提示' , res.msg , 'error' )     loading ().close ()        }) } 
 
storage 的封装utils文件夹下创建storage.ts
export  const  storage = {         set (key: string, value: any ) {         localStorage .setItem (key, JSON .stringify (value))     },          get<T>(key : string) {         const  value = localStorage .getItem (key)         if  (value && value != "undefined"  && value != "null" ) {             return  <T>JSON .parse (value)         }     },          remove (key: string ) {         localStorage .removeItem (key)     } }; export  const  sessionStorage = {         set (key: string, value: any ) {         window .sessionStorage .setItem (key, JSON .stringify (value))     },          get<T>(key : string) {         const  value = window .sessionStorage .getItem (key)         if  (value && value != "undefined"  && value != "null" ) {             return  JSON .parse (value)         }         return  null      },          remove (key: string ) {         window .sessionStorage .removeItem (key)     } } 
 
使用: 
import  { sessionStorage, storage } from  '@/utils/storage'  storage.set ('token' , 'token' ) storage.get ('token' ) storage.remove ('token' , 'token' ) 
 
utils 的封装(长期完善)utils文件夹下创建utils.ts
import  router from  "@/router" ;import  { ElLoading , ElNotification  } from  'element-plus' ;import  { createPinia } from  "pinia" ;export  const  pinia = createPinia ()export  function  routerParam ( ){    return  router.currentRoute .value .query  } const  loadOptions = {    lock : false ,     text : '加载中...' ,     background : 'rgba(255, 255, 255, 0.5)' , } export  function  loading (options = loadOptions ){    return  ElLoading .service (options) } export  function  notification (title: string = '提示' , message: string, type: 'success'  | 'error'  | 'warning'  | 'info'  ){    ElNotification ({         title,         message,         type,     }) } export  function  parseUrl (url: '' , params: any ) {    let  arr = [];     let  string = '' ;     for  (let  i in  params) {         arr.push (i + "="  + params[i]);     }     string = "/pages/"  + url;     if  (arr.length  > 0 ) {         string += "?"  + arr.join ("&" );     }     return  string; } export  function  isDataType (data: any, type: any ) {    return  Object .prototype  .toString .call (data) === '[object '  + type + ']' ; } export  function  in_array (search: any, array: [] ) {    let  flag = false ;     for  (let  i in  array) {         if  (array[i] == search) {             flag = true ;             break ;         }     }     return  flag; }