赞
踩
三方库是开发者在系统能力的基础上进行了一层具体功能的封装,对其能力进行拓展,提供更加方便的接口,提升开发效率的工具
按照其开源属性分为两类:开源三方库和 内部三方库
以@ohos_axios 为例子
在ide中的终端中输入:
ohpm install @ohos/axios
import axios from '@ohos/axios' interface userInfo{ id: number name: string, phone: number } // 向给定ID的用户发起请求 axios.get<userInfo, AxiosResponse<userInfo>, null>('/user?ID=12345') .then((response: AxiosResponse<userInfo>)=> { // 处理成功情况 console.info("id" + response.data.id) console.info(JSON.stringify(response)); }) .catch((error: AxiosError)=> { // 处理错误情况 console.info(JSON.stringify(error)); }) .then(()=> { // 总是会执行 }); // 上述请求也可以按以下方式完成(可选) axios.get<userInfo, AxiosResponse<userInfo>, null>('/user', { params: { ID: 12345 } }) .then((response:AxiosResponse<userInfo>) => { console.info("id" + response.data.id) console.info(JSON.stringify(response)); }) .catch((error:AxiosError) => { console.info(JSON.stringify(error)); }) .then(() => { // 总是会执行 }); // 支持async/await用法 async function getUser() { try { const response:AxiosResponse = await axios.get<string, AxiosResponse<string>, null>(this.getUrl); console.log(JSON.stringify(response)); } catch (error) { console.error(JSON.stringify(error)); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。