首页 > 其他分享 >JSON to TS 转化 Axios 请求类型约束

JSON to TS 转化 Axios 请求类型约束

时间:2023-01-09 14:44:45浏览次数:47  
标签:Axios string res TS IFastData JSON let data 定义

JSON to TS 扩展插件的使用

作用场景

作用是快速转化React+TS的Axios拿回数据的类型约束定义

 

步骤一.获取数据

当页面中发起请求成功,获取到接口所携带的数据时,

const HomeData = async () => {
       let res = await HomeApi()
       let data = res.data as IFastData
  }
  • 此时通过 data 对象来接收 res.data

  • as IFastData 意为限定接收数据的类型约束,是个自定义的名称

 

 

步骤二:通过Log复制所有数据

let data = res.data as IFastData
console.log(data)
  • 此时我们会获取到很多很多的数据,其中会包括数组,对象,字符串,布尔值等等非常多的类型

console.log(data)的预览

collect: 20
exam: {id: 1, pid: null, businessLevel: null, numberLevel: 1, title: 'Java面试题', …}
exemItems: (1) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, ]
study: 24
wrong: 105

可以看到data里还有数组和对象,如果进行手动的类型约束显得格外的困难,此时就需要借助到插件来进行快速转换了

 

步骤三:复制数据进行转化

  1. 创建一个VScode的临时空文件(不需要保存)

  2. 将刚刚data中的数据全部复制进去

  3. 插件提供了两种方法:

    1从剪贴板转换(Shift+Ctrl+Alt+V)
    2 从所选内容转换 (Shift+Ctrl+Alt+S)
  4. 一般选取第二种方式:进行转换后会生成一个转化后的TS文件

 

步骤四:将转换好的类型文件放到页面中

引入类型约束文件有两种方式:

一:本文件形式

此种方式就是十分简单的将定义的词条直接丢在 本页面的最末尾

export default function index({ }: Props) {
   //当需要一个响应式数据的的数组时也可以采取这样的定义方式
   const [dataArray, setdataArray] =useState<Array<IFastRecord>>()
   const GETFastDataAction = async () => {
       let res = await GETFastDataApi()
       //此时使用文件定义类型 as IFastData
       let data = res.data as IFastData
  }

   useEffect(() => {
       GETFastDataAction()
  }, [])

   return (
       <div>
          .......
       </div >
)}

interface IFastData {
   //可以看到 IFastRecord 此时是下一个接口的名称,就意为着可以进行嵌套定义
   records: IFastRecord[];
   total: number;
   size: number;
   orders: any[];
   hitCount: boolean;
}

interface IFastRecord {
   id: number;
   pid?: any;
   title: string;
   info: string;
   actionCode: string;
   actionType: string;
   actionName: string;
}

 

 

二:引入配置文件形式

引入

就是将上方的定义名进行封装打包到指定的文件夹后在进行引入

//引入接口
import { IFastRecord,IFastData } from "../../until/index"

...
const [dataArray, setdataArray] =useState<Array<IFastRecord>>()
   const GETFastDataAction = async () => {
       let res = await GETFastDataApi()
       //此时使用文件定义类型 as IFastData
       let data = res.data as IFastData
  }

 

导出

而在接口文件夹中我们也需要进行接口的导出

../../until/index

//定义fast返回的数据类型
export interface FastDatastate {
   actionCode: string,
   actionName: string,
   actionType: string,
   ......
}

//定义home返回的数据类型
export interface HomeDatastate {
   collect: string,
   study: string,
   wrong: string,
   .....    
}
 

标签:Axios,string,res,TS,IFastData,JSON,let,data,定义
From: https://www.cnblogs.com/Dollom/p/17037003.html

相关文章

  • echarts tooltip pie {b}展示不全
    echartstooltippie{b}展示不全tooltip:{show:true,trigger:'item',formatter:'{b}:{c}{d}%'},原因:后端......
  • rails snippets
    snippetartassert_redirected_to${1:action}:'${2:index}'snippetartnpassert_redirected_to${1:parent}_${2:child}path(${3:@$1},${0:@$2})snippetartnppass......
  • ruby snippets
    snippetenc#encoding:utf-8snippetfrozen#frozen_string_literal:truesnippet#!#!/usr/bin/envrubyNewBlocksnippet=b=beginrdoc${0}=endsnippetpr......
  • scss snippets
    extendscsssnippet$$${1:variable}:${0:value};snippetimp@import'${0}';snippetmix@mixin${1:name}(${2}){${0}}snippetinc@include${1:mixin}(${2})......
  • html snippets
    SomeusefulUnicodeentitiesNon-BreakingSpacesnippetnbs ←snippetleft←→snippetright→↑snippetup↑↓snippetdown↓↩snippetreturn↩......
  • css snippets
    snippet."selector{}"${1}{${2:${VISUAL}}}${0}snippet!"!important"!importantsnippetbdi:m+-moz-border-image:url(${1})${2:0}${3:0}${4:0}${5:0}......
  • markdown snippets
    MarkdownIncludesoctopress(http://octopress.org/)snippetsThesuffixcstandsfor"Clipboard".snippet[${1:text}snippet[*${1:link}snippet[c${1:link}......
  • 【技术分享】Windows平台低延迟RTMP、RTSP播放器接口设计探讨
    背景我们看过了太多介绍RTSP、RTMP播放相关的技术资料,大多接口设计简约,延迟和扩展能力也受到一定的局限,好多开发者希望我们能从接口设计的角度,大概介绍下大牛直播SDK关于RTM......
  • 【车间调度】基于GA/PSO/SA/ACO/TS优化算法的车间调度比较(Matlab代码实现)
    目录1概述2 FJSP描述3运行结果3.1main1运行结果3.2main2运行结果4Matlab代码 5参考文献6写在最后1概述柔性作业车间调度问题(FlexibleJobshopSched-ulingPro......
  • struts2_day01
    Struts2第一天Struts2的学习路线1.Struts2的入门:主要是学习Struts2的开发流程(Struts2的开发流程、常见的配置、Action类的编写)2.Struts2的Servlet的API、参数封装和拦截器......