vue使用axios 遇到的坑

起步:
配置axios

  • 在main.js文件中
import axios from 'axios'
Vue.prototype.$http = axios
  • 在config/index.js
 proxyTable: {
      '/api':{
          target:'http://QuanDD.com',  //一定要加上http://  (不然域名回事你前端项目的域名)
          changeOrigin:true  //允许跨域,
          pathRewrite: {
				'^/list': '' //这里用'/list'代替target里面的地址,后面的组建中直接使用list代替
			}
      }

注意配置index.js的时候会遇到
proxyTable一系列问题问题已经卸载注释里面,还有一个坑就是配置完proxyTable之后不起作用,有以下解决办法:

  1. proxTabtle 配置错误,比如:pathRewrite中配置 和自己网络请求中url写法不匹配
  2. 电脑开着全局代理翻墙
  3. 删掉node_modules 目录,重新 npm install 安装
  4. 重启电脑
  • 在组件中使用
this.$http.get('/api/v1/listA', {
                    "op": "update_card_num",
                })
                    .then(response=> {
                        //如果接口走成功就执行这里
                        console.log(852)

                    }).catch(function (error) {
                    //接口失败,也就是state不是200的时候,走这里
                    console.log(96)
                });

猜你喜欢

转载自blog.csdn.net/qq_26282869/article/details/86219616