首页 > 编程知识 正文

typescript vue3,vue请求数据axios

时间:2023-05-05 19:59:10 阅读:11186 作者:1713

vue3.0 axios封装在src目录下创建文件,类似于2.0

config.jsexportconstmethod={ get : ' get ',POST: 'post ',PUT: 'put ',DELETE:'delete'}; exportconstpath={ get JSON : '/mock/index.JSON ',detail: '/mock/detail.json ',}; core.js

导入axios from ' axios '; 导入{ method } from './config '; 常数实例=axios.create ({ base URL : ' http://localhost 33608080 ',timeout: 5000,} ); //let加载=null; //请求拦截器instance.interceptors.request.use (function (config ) { return config;function(error )//对请求错误做些什么returnpromise.reject ); ); //响应拦截器instance.interceptors.response.use (function (response ) { return response;function(error )//为响应错误做点什么returnpromise.reject ); ); exportfunction request (方法,url,parmas ) switch (method ) case method.get :返回get (URL,parmas ); case method.post :返回post (URL,parmas ); case method.put : return put (URL,parmas ) case method.delete : return delete (URL,parmas ) }功能get (URL,parmas ) }functionput(URL,params ) return instance.put (URL,params ); }functiondelete(URL,params ) return instance.delete (URL,params ); } index.js

导入{ method,PATH } from './config '; 导入{请求}来自'./核心'; constAPIclient={getJSON(params ) returnrequest ) method.get,PATH.getJson,Params;详细(Params ) returnrequest ) method.get,PATH.detail,Params ); (、); 导出默认API客户端; 在main.js上部署

importappfrom './app.vue ' importrouterfrom './router ' importstorefrom './store ' importvantfrom ' vant '; 导入' vant/lib/index.CSS '; importhttpfrom './network/index '//移动端为import 'lib-flexible/flexible '; constapp=createapp(app ) app.config.global properties.$ http=http app.use (store ).use ) router.use ) http具体内容

导入{ getcurrentinstance=} from ' vue '; const { ctx }=getCurrentInstance (; //getCurrentInstance (; vue3中不再有this实例,因此单独部署ctx.$http.getJson ().this () ) ) RES )={ console.log (RES.data.data ) ) state.icon list=RES.data.data.icon list; state.recommend list=RES.data.data.recommend list; state.swipe rlist=RES.data.data.swipe rlist; state.weekend list=RES.data.data.weekend list; );

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。