vue安装 axios

10 篇文章 0 订阅

安装

npm安装
npm install vue
直接引入
<script src="https://cdn.jsdelivr.net/npm/vue"></script>

vue init webpack mydemo

切换到项目目录
cd mydemo

安装模块
npm install
  它根据package.json的配置表进行安装,安装完后会在mydemo下多一个文件夹node_modules,这里的文件对应着package.json里的配置信息。
  
输入命令
npm run dev
  在浏览器输入地址http://localhost:8080,看到如下页面,说明大功告成,一个Vue项目已经初始化完成!
  
使用element-ui
npm i element-ui -S

完整引入
在 main.js 中写入以下内容:

 

import Vue from 'vue';
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import App from './App.vue';

Vue.use(ElementUI);

new Vue({
  el: '#app',
  render: h => h(App)
});

安装Vue Devtools调试工具

  • 在github下载devtools源码
    https://github.com/vuejs/vue-devtools

  • 在vue-devtools-master工程中执行命令
    执行cnpm install,下载依赖

  • 执行npm run build,编译源程序

  • 编译完成后修改数据
    修改shells >chrome目录下的mainifest.json 中的persistant为true

  • 设置谷歌浏览器的扩展程序
    勾选开发者模式,然后将刚刚编译后的工程中的shells目录下,chrome的整个文件夹直接拖拽到当前浏览器中,并选择启用,即可将插件安装到浏览器。

引入全局函数

新建common.js文件

 

var fun = function () {
  console.log('hello')
}
export default fun

在main.js中引入
import fun from '@/js/common.js'
Vue.prototype.$pubFuc = fun

关闭elint

在配置文件中,注释掉规则

 

const createLintingRule = () => ({
  // test: /\.(js|vue)$/,
  // loader: 'eslint-loader',
  // enforce: 'pre',
  // include: [resolve('src'), resolve('test')],
  // options: {
  //   formatter: require('eslint-friendly-formatter'),
  //   emitWarning: !config.dev.showEslintErrorsInOverlay
  // }
})

axios

npm install axios

Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中。

vue中使用axios
1.安装axios

npm:

$ npm install axios -S
cdn:

<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
2.配置axios

在项目中新建api/index.js文件,用以配置axios

api/index.js

 

import axios from 'axios';

let http = axios.create({
  baseURL: 'http://localhost:8080/',
  withCredentials: true,//是否开启跨域,更改header的时候就要可以打开
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
  },
  transformRequest: [function (data) {
    let newData = '';
    for (let k in data) {
      if (data.hasOwnProperty(k) === true) {
        newData += encodeURIComponent(k) + '=' + encodeURIComponent(data[k]) + '&';
      }
    }
    return newData;
  }]
});

function apiAxios(method, url, params, response) {
  http({
    method: method,
    url: url,
    data: method === 'POST' || method === 'PUT' ? params : null,
    params: method === 'GET' || method === 'DELETE' ? params : null,
  }).then(function (res) {
    response(res);
  }).catch(function (err) {
    response(err);
  })
}
// request拦截器
service.interceptors.request.use(config => {
  console.log(store.getters.token)
  if (store.getters.token) {
    console.log(getToken())
    config.headers['X-Token'] = getToken() // 让每个请求携带自定义token 请根据实际情况自行修改
    var token = getToken()
    Object.assign(config.headers, { 'token': token })
  }
  return config
}, error => {
  // Do something with request error
  console.log(error) // for debug
  Promise.reject(error)
})
// respone拦截器
service.interceptors.response.use(
  response => {
    /**
     * code为非20000是抛错 可结合自己业务进行修改
     */
    console.log(response.data)
    const res = response.data
    if (res.code !== 20000) {
      Message({
        message: res.message,
        type: 'error',
        duration: 5 * 1000
      })
 
      // 50008:非法的token; 50012:其他客户端登录了;  50014:Token 过期了;
      if (res.code === 50008 || res.code === 50012 || res.code === 50014) {
        MessageBox.confirm('你已被登出,可以取消继续留在该页面,或者重新登录', '确定登出', {
          confirmButtonText: '重新登录',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          store.dispatch('FedLogOut').then(() => {
            location.reload() // 为了重新实例化vue-router对象 避免bug
          })
        })
      }
      return null
    } else {
      return response.data
    }
  },
  error => {
    if (error.message === 'Network Error' && error.config.url.endsWith('/license')) {
      Message({
        message: '无法连接到本地代理程序,请确认代理程序是否运行正常!',
        type: 'error',
        duration: 5 * 1000
      })
    } else {
      console.log(error + ' ' + error.config.url) // for debug
      Message({
        message: error.message + ' ' + error.config.url,
        type: 'error',
        duration: 5 * 1000
      })
    }
    return Promise.reject(error)
  }
export default {
  get: function (url, params, response) {
    return apiAxios('GET', url, params, response)
  },
  post: function (url, params, response) {
    return apiAxios('POST', url, params, response)
  },
  put: function (url, params, response) {
    return apiAxios('PUT', url, params, response)
  },
  delete: function (url, params, response) {
    return apiAxios('DELETE', url, params, response)
  }
}

这里的配置了POST、GET、PUT、DELETE方法。并且自动将JSON格式数据转为URL拼接的方式

同时配置了跨域,不需要的话将withCredentials设置为false即可

并且设置了默认头部地址为:http://localhost:8080/,这样调用的时候只需写访问方法即可

3.使用axios

注:PUT请求默认会发送两次请求,第一次预检请求不含参数,所以后端不能对PUT请求地址做参数限制

首先在main.js中引入方法

 

import Api from './api/index.js';
Vue.prototype.$api = Api;

然后在需要的地方调用即可

 

this.$api.post('user/login.do(地址)', {
    "参数名": "参数值"
}, response => {
     if (response.status >= 200 && response.status < 300) {
        console.log(response.data);\\请求成功,response为成功信息参数
     } else {
        console.log(response.message);\\请求失败,response为失败信息
     }
});


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值