≡
  • 网络编程
  • 数据库
  • CMS技巧
  • 软件编程
  • PHP笔记
  • JavaScript
  • MySQL
位置:首页 > 网络编程 > vue.js

vue项目中api接口管理总结

人气:304 时间:2019-04-10

这篇文章主要为大家详细介绍了vue项目中api接口管理总结,具有一定的参考价值,可以用来参考一下。

感兴趣的小伙伴,下面一起跟随四海网的小编两巴掌来看看吧!

默认vue项目中已经使用vue-cli生成,安装axios,基于element-ui开发,axiosconfig目录和api目录是同级,主要记录配置的相关。

 

1. 在axiosconfig目录下的axiosConfig.js

 

代码如下:


import Vue from 'vue'
import axios from 'axios'
import qs from 'qs'
import { Message, Loading } from 'element-ui'
// 响应时间
axios.defaults.timeout = 5 * 1000
// 配置cookie
// axios.defaults.withCredentials = true
// 配置请求头
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'
// 静态资源
Vue.prototype.$static = ''

// 配置接口地址
axios.defaults.baseURL = ''
var loadingInstance
// POST传参序列化(添加请求拦截器)
axios.interceptors.request.use(
 config => {
  loadingInstance = Loading.service({
   lock: true,
   text: '数据加载中,请稍后...',
   spinner: 'el-icon-loading',
   background: 'rgba(0, 0, 0, 0.7)'
  })
  if (config.method === 'post') {
   config.data = qs.stringify(config.data)
  }
  return config
 },
 err => {
  loadingInstance.close()
  Message.error('请求错误')
  return Promise.reject(err)
 }
)
// 返回状态判断(添加响应拦截器)
axios.interceptors.response.use(
 res => {
  if (res.data.code === 200) {
   loadingInstance.close()
   return res
  } else {
   loadingInstance.close()
   Message.error(res.data.msg)
  }
 },
 err => {
  loadingInstance.close()
  Message.error('请求失败,请稍后再试')
  return Promise.reject(err)
 }
)
// 发送请求
export function post (url, params) {
 return new Promise((resolve, reject) => {
  axios
   .post(url, params)
   .then(
    res => {
     resolve(res.data)
    },
    err => {
     reject(err.data)
    }
   )
   .catch(err => {
    reject(err.data)
   })
 })
}
export function get (url, params) {
 return new Promise((resolve, reject) => {
  axios
   .get(url, {
    params: params
   })
   .then(res => {
    resolve(res.data)
   })
   .catch(err => {
    reject(err.data)
   })
 })
}

 

2. 在api目录下的index.js,api1.js,api2.js

 

代码如下:


api1.js
import { post } from '../axiosconfig/'
export default {
  login(params) {
    return post('/users/api/login', params)
  }
}
api2.js
import { post } from '../axiosconfig/'
export default {
  regist(params) {
    return post('/users/api/regist', params)
  }
}
index.js
import user from './api1.js'
import active from './api2.js'
export default {
 api1,
 api2
}

 

3. main.js 配置

 

代码如下:


import api from './api/'
Vue.prototype.$api = api

 

4. 在组件中使用

 

代码如下:


登录组件中
doLongin() {
 let params={}
 this.$api.api1.login(params).then(res => {
  console.log(res)
 })
}
注册组件中
doRegist() {
 let params={}
 this.$api.api2.regist(params).then(res => {
  console.log(res)
 })
}



 

本文来自:http://www.q1010.com/184/4914-0.html

注:关于vue项目中api接口管理总结的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。

关键词:vue.js

您可能感兴趣的文章

  • vue 之 .sync 修饰符示例分析
  • Vue下滚动到页面底部无限加载数据的示例代码
  • vue.js与element-ui实现菜单树形结构的解决方法
  • 用Vue写一个分页器的示例代码
  • 在vue-cli项目中使用bootstrap的方法示例
  • nodejs读取并去重excel文件
  • vue源码解析之事件机制原理
  • vue iview实现动态路由和权限验证功能
  • Vue 去除路径中的#号
  • vue-cli3.0 特性解读
上一篇:使用vuex的state状态对象的5种方式
下一篇:vue-cli3.0 特性解读
热门文章
  • Vue 报错TypeError: this.$set is not a function 的解决方法
  • vue实现动态添加数据滚动条自动滚动到底部的示例代码
  • vue项目设置scrollTop不起作用(总结)
  • vue项目中使用vue-i18n报错的解决方法
  • iview实现select tree树形下拉框的示例代码
  • 分析关于element级联选择器数据回显问题
  • vue项目打包后打开页面空白解决办法
  • 解决element ui select下拉框不回显数据问题的解决
  • element-ui table span-method(行合并)的实现代码
  • element-ui 设置菜单栏展开的方法
  • 最新文章
    • 理解vue ssr原理并自己搭建简单的ssr框架
    • vue favicon设置以及动态修改favicon的方法
    • vue-router启用history模式下的开发及非根目录部署方法
    • 从零开始在NPM上发布一个Vue组件的方法步骤
    • Element input树型下拉框的实现代码
    • Vue 报错TypeError: this.$set is not a function 的解决方法
    • Vue.js组件高级特性实例分析
    • 浅谈VueJS SSR 后端绘制内存泄漏的相关解决经验
    • 分析Vue.js自定义tipOnce指令用法实例
    • 浅谈vuex actions和mutation的异曲同工

四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。