当前位置: 首页 > news >正文

做一个网站需要到哪里做郑州seo线上推广系统

做一个网站需要到哪里做,郑州seo线上推广系统,电子商务是电商吗,东营人才网仓库地址:https://github.com/buguniao5213/LuArch 1、安装axios npm install axios2、创建文件 先创建一个文件夹: ├── src/ │ ├── api/ │ │ ├── index.ts/ #编写axios封装代码 │ │ └── example.ts/ #定义…

仓库地址:https://github.com/buguniao5213/LuArch

1、安装axios

npm install axios

2、创建文件

先创建一个文件夹:

├── src/
│   ├── api/        
│   │   ├── index.ts/   #编写axios封装代码    
│   │   └── example.ts/ #定义向后端服务器发送请求的模块`
├── publix/
│   ├── json/        
│   │   └── example.json/   #模拟get接口获取到的数据`

3、导入申明

导入axios库和相关类型

import axios, { type AxiosInstance, type AxiosRequestConfig } from 'axios';

4、基础url定义

这个后面放到vite的缓建变量配置中(.env)

const BASE_URL = '/'

5、定义request类

要素如下:

export class Request {private instance: AxiosInstance;private baseConfig: AxiosRequestConfig = { baseURL: BASE_URL, timeout: 60000 };public constructor() {//...    }public request() {//...    }public get() {//...    }//...//...
}

a)、private instance:私有属性instance:Axios实例

b)、private baseConfig:基本配置,包括基础URL和超时时间

c)、public constructor:构造函数

创建Axios实例,合并基本配置和传入配置

设置请求拦截器:可以添加token等认证信息

设置响应拦截器:处理相应数据,根据状态码决定返回数据或报错误信息

d)、请求方法设置:

request:通用请求方法。

get:GET请求方法。

post: POST 请求方法

put: PUT 请求方法

delete: DELETE 请求方法

完整代码如下:

index.ts:

export class Request {private instance: AxiosInstance;private baseConfig: AxiosRequestConfig = { baseURL: BASE_URL, timeout: 60000 };public constructor(config: AxiosRequestConfig) {this.instance = axios.create(Object.assign(this.baseConfig, config));this.instance.interceptors.request.use((config: any) => {// 配置处理逻辑// const token = 'tokentoken';return config;},(error: any) => {return Promise.reject(error);})this.instance.interceptors.response.use((res: any) => {if(res.data.code === 200) {return res.data.data;}else {// 错误code处理return "发生错误";}},(error: any) => {return Promise.reject(error);})}public request<T = any>(config: AxiosRequestConfig): Promise<T> {return this.instance.request(config);}public get<T = any>(url: string, params?: any, config?: AxiosRequestConfig): Promise<T> {return this.instance.get(url, {params, ...config});}public post<T = any>(url: string, data?: any, config?: AxiosRequestConfig): Promise<T> {return this.instance.post(url, data, config);}public put<T = any>(url: string, data?: any, config?: AxiosRequestConfig): Promise<T> {return this.instance.put(url, data, config)}public delete<T = any>(url: string, params?: any, config?: AxiosRequestConfig): Promise<T> {return this.instance.delete(url, {params, ...config});}}

6、创建实例,并导出为Axios

export const Axios = new Request({baseURL: BASE_URL,
});

7、使用

a)、封装一个HTTP请求

example.ts:

import { Axios } from '@/api'export function HTLLOWORD() {return Axios.get<any>('/json/example.json')
}

example.json:

{   "code": 200,"data": "hello word"
}

b)、调用

import { HTLLOWORD } from '@/api/example'const getTest = () => {HTLLOWORD().then(res => {console.log(res)})
}getTest();
http://www.hkea.cn/news/411203/

相关文章:

  • 京东pc网站用什么做的如何注册网站怎么注册
  • 长沙商城网站制作seo线下培训课程
  • web网站开发公司网站制作优化排名
  • 这么做3d网站企业邮箱网页版
  • 瑞安网站建设公司关键词排名网络推广
  • 南京学做网站友情链接检查工具
  • 参考文献网站开发百度重庆营销中心
  • 如何做微信ppt模板下载网站企业网页设计公司
  • 做b2b网站百度点击快速排名
  • 网站怎么做移动图片不显示不出来吗芭嘞seo
  • 旅游网站建设服务器ip域名解析
  • 企业网站建设三个原则百度指数资讯指数是指什么
  • 房地产集团网站建设方案软文文案案例
  • 阜蒙县建设学校网站是什么北京seo编辑
  • 珠海建设局网站十大经典事件营销案例分析
  • 创建网站开发公司互联网推广引流是做什么的
  • 万盛集团网站建设seo网站推广全程实例
  • 做教育的网站需要资质吗网站怎么开发
  • 微网站怎么做滚动中国万网域名注册官网
  • 个人如何免费建网站seo在线优化工具 si
  • 双线主机可以做彩票网站吗网络推广合作协议
  • 做外贸的b2b网站域名批量查询系统
  • 建设网站需要哪些职位网站建设策划书
  • 苏州网站建设哪里好网站点击排名优化
  • 网站建设收费标准策划百度推广关键词越多越好吗
  • 网站怎么做更新吗如何建立网页
  • 国外建设工程招聘信息网站tool站长工具
  • 专业做相册书的网站电商网站建设制作
  • 银川网站开发公司电话东莞网
  • 环境保护局网站管理制度建设百度指数的主要功能有