当前位置:   article > 正文

axios二次封装_get: async (option: any) => { const res =

get: async (option: any) => { const res = await request({ method:

目录

普通发送axios请求

一次封装 

二次封装

注意事项 !!

         1.  async、await 查看报错信息

2.axios基地址的baseURL

3. 导出函数时注意写return 


普通发送axios请求

  1. //App.vue
  2. <script>
  3. // 在次之前 一定要下载 axios哦
  4. import axios from 'axios'
  5. export default ({
  6. methods: {
  7. async getUrl () {
  8. const res = await axios({
  9. url: 'http://api-xxxx.net/app/v1_0/user/channels'
  10. })
  11. console.log(res)
  12. }
  13. // async getUrl2 () {
  14. // axios({
  15. // url: 'http://api-xxxx.net/app/v1_0/user/channels',
  16. // methods: 'GET'
  17. // }).then(res => console.log(res)).catch(err=>console.log(err))
  18. // }
  19. },
  20. created () {
  21. this.getUrl()
  22. }
  23. })
  24. </script>

缺点: 虽然说也能发送请求成功,但是如果我想每次调用就每次都在组件里面写一次 ,使无用代码量增加,也不适于其他人阅读

所以我们接下来进行封装

一次封装 

我们在until/index.js进行封装

  1. import axios from 'axios'
  2. export const request = axios.create({
  3. baseURL: 'http://xxxx.net'
  4. })

    注意点:  axios.create() 是创建一个新的axios实例   每个都可以配置不同的基地址服务器   解决了请求2个及更多的服务器的情况

  1. // App.vue
  2. import { request } from './unitls/index'
  3. export default ({
  4. methods: {
  5. async getUrl () {
  6. const res = await request({
  7. url: '/app/v1_0/user/channels'
  8. })
  9. console.log(res)
  10. }
  11. },
  12. created () {
  13. this.getUrl()
  14. }
  15. })

缺点:我们这样在组件中还要写服务器接口 是不是不太好看呐       并且 如果我请求多次不同的接口是不是每个地方的url 都要修改

我们改为这样  const res = await request() 是不是更加简单明了 逻辑清晰  so~我们进行第二次封装

二次封装

把组件中的url抽离出来    until/index.js不需要动 

  1. // until/index.js
  2. import axios from 'axios'
  3. export const login = axios.create({
  4. baseURL: 'http://xxxx'
  5. })

新建一个api文件夹 api/login.js

  1. import { login } from '../unitls/index'
  2. export const loginUser = () => {
  3. return login({
  4. url: '/app/v1_0/user/channels'
  5. })
  6. }

接下来 就可以直接使用了

  1. <script>
  2. import { loginUser } from './API/login.js'
  3. export default ({
  4. methods: {
  5. async getUrl () {
  6. try {
  7. console.log(1)
  8. const res = await loginUser()
  9. console.log(res)
  10. } catch (error) {
  11. console.log(error)
  12. }
  13. }
  14. },
  15. created () {
  16. this.getUrl()
  17. }
  18. })
  19. </script>

好处:

  • 使代码逻辑清晰简单  在组件中方便其他人阅读
  • 使代码复用性更强,是不是换个服务器和接口 只需要cv啊!!
  • 修改服务器基地址和接口是不是一次修改 其他都不用修改了呢

注意事项 !!

 1.  async、await 查看报错信息

我们知道普通axios 返回一个 promise对象  可以继续.then().catch()... 但是async呢

  1. methods: {
  2. async getUrl () {
  3. try {
  4. const res = await loginUser()
  5. console.log(res)
  6. } catch (error) {
  7. console.log(error)
  8. }
  9. }
  10. },
  11. created () {
  12. this.getUrl()
  13. }

使用try{ 代码块 } (err) { console.log('err') }    注意  catch 一定要知道  只要catch 不只是接收axios的错误信息  只要 try中有错误 catch 就会执行 

2.axios基地址的baseURL

  1. export const login = axios.create({
  2. baseUrl: 'http://xxxx'
  3. //timeout: 5000 请求超时事件最长5秒
  4. })

注意上面的代码中baseURL是不是rl没有大写,如果没大写说明地址根本拿不到(等于没写) 就会以当前的8080端口去拼接接口 

    最后发现404了   一定注意 出现8080端口百分之八十是地址写错了

3. 导出函数时注意写return 

  1. import { login } from '../unitls/index'
  2. // 本代码错误 注意别复制
  3. export const loginUser = () => {
  4. login({
  5. url: '/app/v1_0/user/channels'
  6. })
  7. }

 

这里虽然已经显示发送请求成功并且是 200 但是 我们拿到的值呢

最后发现却是undefined  比如说 我们定义了一个函数  我们没有return 那么这个函数拿到的是不是undefined       soga!一定要写return

代码片段

  1. {
  2. // Place your 全局 snippets here. Each snippet is defined under a snippet name and has a scope, prefix, body and
  3. // description. Add comma separated ids of the languages where the snippet is applicable in the scope field. If scope
  4. // is left empty or omitted, the snippet gets applied to all languages. The prefix is what is
  5. // used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
  6. // $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders.
  7. // Placeholders with the same ids are connected.
  8. // Example:
  9. "try catch async await": {
  10. "scope": "javascript,typescript",
  11. "prefix": "async", // 快捷键 + tab
  12. "body": [
  13. "async $1(){",
  14. "import { $2 } from '@/api/${4}'",
  15. " try {",
  16. " const res = await $2($3)",
  17. " console.log('$2', res)",
  18. " ${5}// 保存数据",
  19. " } catch (err) {",
  20. " console.log('$2', err)",
  21. " }",
  22. "},"
  23. ],
  24. "description": "飞起来的async await"
  25. }
  26. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/294629
推荐阅读
相关标签
  

闽ICP备14008679号