已配置好的vue全家桶项目router,vuex,api,axios,vue-ls,async/await,less下载即使用

烬余封装
• 阅读 4304

github 地址: https://github.com/liangfengbo/vue-cli-project 点击进入

vue-cli-project

  • 已构建配置好的vuejs全家桶项目,统一管理后端接口 | 获取数据 | 请求数据,已包含vue-router,vuex,api,axios. webpack, 储存用vue-ls, 异步async/await, css less. 下载即使用项目开发。
  • 喜欢或对你有帮助的话请点star✨✨,Thanks.
A Vue.js project

使用

# 安装服务
npm install

# 启动服务
npm run dev

说明

src架构

├── App.vue
├── api
│   ├── doctor.js
│   └── fetch.js
├── assets
│   └── logo.png
├── components
│   └── HelloWorld.vue
├── libs
│   └── util.js
├── main.js
├── router
│   └── index.js
├── store
│   ├── index.js
│   ├── modules
│   └── mutation-types.js
└── views
    └── doctor

处理数据页面这2大块,把数据和页面分离,在vuex里面做请求数据,页面只需要做调用数据。

请求接口这块再细分,接口和请求接口分开,我使用了最新的async/await函数做数据请求

api文件夹 主要放后端提供的接口,如

import fetch from './fetch';

export default {
  // 获取医生列表
  list(params) {
    return fetch.get('/doctor/list', params)
  },

  // 获取医生详情信息
  detail(id) {
    return fetch.post('/doctor/detail/' + id);
  },
}

fetch.js 文件是封装axios请求,以及请求处理,和http状态码的等处理

import Util from '../libs/util'
import qs from 'qs'
import Vue from 'vue'

Util.ajax.defaults.headers.common = {
  'X-Requested-With': 'XMLHttpRequest'
};

Util.ajax.interceptors.request.use(config => {
  /**
   * 在这里做loading ...
   * @type {string}
   */

  // 获取token
  config.headers.common['Authorization'] = 'Bearer ' + Vue.ls.get("web-token");
  return config

}, error => {
  return Promise.reject(error)

});

Util.ajax.interceptors.response.use(response => {

  /**
   * 在这里做loading 关闭
   */

    // 如果后端有新的token则重新缓存
  let newToken = response.headers['new-token'];

  if (newToken) {
    Vue.ls.set("web-token", newToken);
  }

  return response;

}, error => {
  let response = error.response;
  if (response.status == 401) {
    // 处理401错误

  } else if (response.status == 403) {
    // 处理403错误

  } else if (response.status == 412) {
    // 处理412错误

  } else if (response.status == 413) {
    // 处理413权限不足

  }

  return Promise.reject(response)

});

export default {
  post(url, data) {

    return Util.ajax({
      method: 'post',
      url: url,
      data: qs.stringify(data),
      timeout: 30000,
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
      }
    })
  },

  get(url, params) {
    return Util.ajax({
      method: 'get',
      url: url,
      params,
    })
  },

  delete(url, params) {
    return Util.ajax({
      method: 'delete',
      url: url,
      params
    })
  },

  put(url, data) {

    return Util.ajax({
      method: 'put',
      url: url,
      data: qs.stringify(data),
      timeout: 30000,
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
      }
    })
  }
}

在vuex里面做请求,比如请求医生列表,用async/await,拿到数据存进store数据里面

├── index.js
├── modules
│   └── doctor.js
└── mutation-types.js

import doctor from '../../api/doctor'
import * as types from '../mutation-types'

const state = {
  // 医生列表
  doctorList: [],
  // 医生详情信息
  doctorDetail: null,
};

const mutations = {
  // 设置医生列表
  [types.SET_DOCTOR_LIST](state, data) {
    state.doctorList = data
  },
  // 设置医生详情信息
  [types.SET_DOCTOR_DETAIL](state, data) {
    state.doctorDetail = data
  },
};

const actions = {

  /**
   * 获取医生顾问列表
   * @param state
   * @param commit
   * @param params
   * @returns {Promise<void>}
   */
  async getDoctorList({state, commit}, params) {
    let ret = await doctor.list(params);
    commit(types.SET_DOCTOR_LIST, ret.data.data);
  },

  /**
   * 获取医生详情信息
   * @param state
   * @param commit
   * @param id 医生ID
   * @returns {Promise<void>}
   */
  async getDoctorDetail({state, commit}, id) {
    let ret = await doctor.detail(id);
    commit(types.SET_DOCTOR_DETAIL, ret.data.data);
  }
};

export default {
  state,
  actions,
  mutations
}

在页面里需要执行引入:

import {mapActions, mapState} from 'vuex'

代码可以具体看文件的代码

└── doctor
    ├── detail.vue
    └── list.vue

<script>
  import {mapActions, mapState} from 'vuex'

  export default {
    components: {},
    data() {
      return {}
    },
    computed: {
      ...mapState({
        doctorList: state => state.doctor.doctorList,
      })
    },
    async created() {
      // 医生类型
      let params = {type: 'EXP'};
      // 获取医生列表
      await this.getDoctorList(params);
    },
    methods: {
      ...mapActions([
        // 获取医生列表
        'getDoctorList'
      ]),

      // 路由跳转方法
      routeLink(link) {
        this.$router.push({path: link});
      },
    }
  }
</script>

核心就是把数据和页面分离,细分把接口,请求数据用vuex做处理,在页面获取数据都做统一管理项目。可以具体看项目里面的代码。

github 地址: https://github.com/liangfengbo/vue-cli-project 点击进入

点赞
收藏
评论区
推荐文章
Oracle 分组与拼接字符串同时使用
SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(
Wesley13 Wesley13
4年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
九路 九路
5年前
vue-toy: 200行代码模拟Vue实现
vuetoy200行左右代码模拟vue实现,视图渲染部分使用React来代替Sanbbdom,欢迎Star。项目地址:https://github.com/bplok20010/vuetoy(https://github.com/bplok20010/vuetoy)codesandbox示例(https://codes
Easter79 Easter79
4年前
springboot+vue 登录页面(一)
首先了解的技术点是:后台:springboot,mybatis,mybatis逆向工程,mysql,日志前端:nodejs,npm,cnpm,vue,vuecli,webpack,elementui,router,axios开发工具:idea,webstorm该项目前端使用的是vue,目的是实现前后端分离后台:1.选择spr
Chase620 Chase620
4年前
记录Vue项目实现axios请求头带上token
在vue项目中首先npm命令安装axios:npminstallaxiosSaxios的封装使用请求带上token,token通过登录获取存在vuex,为防止刷新丢失token使用持久化vuexpersistedstate插件保存数据npmiSvuexpersistedstateimportpersistedStat
可莉 可莉
4年前
18个常用 webpack插件,总会有适合你的!
!(https://oscimg.oschina.net/oscnet/71317da0c57a8e8cf5011c00e302a914609.jpg)来源| https://github.com/Michaellzg/myarticle/blob/master/webpack/Plugin何为插
Wesley13 Wesley13
4年前
Java 学习资料网站集合
一、开源项目的搜集https://www.jianshu.com/p/6c75174e0f07 https://github.com/flyleft/tip二、简单的开源项目https://github.com/ShawnyXiao/SpringBootMyBatis(这个不错)https://github.com/flyleft/
Stella981 Stella981
4年前
Golang注册Eureka的工具包goeureka发布
1.简介提供Go微服务客户端注册到Eureka中心。点击:github地址(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fgithub.com%2FSimonWang00%2Fgoeureka),欢迎各位多多star!(已通过测试验证,用于正式生产部署)2.原理
Stella981 Stella981
4年前
Linux日志安全分析技巧
0x00前言我正在整理一个项目,收集和汇总了一些应急响应案例(不断更新中)。GitHub地址:https://github.com/Bypass007/EmergencyResponseNotes本文主要介绍Linux日志分析的技巧,更多详细信息请访问Github地址,欢迎Star。0x01日志简介Lin
Stella981 Stella981
4年前
OneFlow 实现强化学习玩 Flappy Bird 小游戏
点击蓝字关注我们GitHub:https://github.com/OneflowInc/oneflow!(https://oscimg.oschina.net/oscnet/2dad9ad45e6b4fca867ca86504292dc0.png)(点击“阅读原文”,即刻进入GitHub仓库!)前言