小肆前几天发了一篇2019年Vue精品开源项目库的汇总,今天小肆要使用的是在UI组件中排行第三的Vuetify。
Vuetify是一个渐进式的框架,彻底根据Material Design规范开发,一共拥有80多个组件,对移动端支持很是好。css
支持SSR(服务端渲染),SPA(单页应用程序),PWA(渐进式Web应用程序)和标准HTML页面。html
vuetify官方文档给出了它具有的几点优点:vue
安装算是比较简单了,在项目目录输入如下命令就OK:ios
vue add vuetify
但这时有一个问题,若是咱们使用默认的icon,index.html
里面引入的是google的连接vuex
<link href='https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900|Material+Icons' rel="stylesheet">
咱们须要替换成国内的vue-cli
https://fonts.cat.net/
今天咱们先用vuetify的语法写一个底部导航的组件,先放代码:axios
<template> <v-card flat> <v-bottom-nav :value="true" fixed color="transparent"> <v-btn color="teal" :to="{path:'/'}" flat> <span>首页</span> <v-icon>home</v-icon> </v-btn> <v-btn color="teal" :to="{path:'/lottery'}" flat> <span>足彩</span> <v-icon>favorite</v-icon> </v-btn> <v-btn color="teal" :to="{path:'/competition'}" flat> <span>赛事</span> <v-icon>place</v-icon> </v-btn> <v-btn color="teal" :to="{path:'/course'}" flat> <span>课程</span> <v-icon>music_video</v-icon> </v-btn> </v-bottom-nav> </v-card> </template>
这里主要用到的是v-bottom-nav
这个API,下面这张图显示了它可用的所有属性:api
上述代码的实际显示效果:app
为了使用方便,咱们改造一下vuex,新建store目录,目录结构以下:框架
更改store.js
import Vue from 'vue' import Vuex from 'vuex' import app from './store/modules/app' import user from './store/modules/user' import getters from './store/getters' Vue.use(Vuex) const store = new Vuex.Store({ modules: { app, user }, getters }) export default store
昨天咱们配置了axios,今天咱们来配置一下全局loading。
RequestLoading.vue
<template> <transition name="fade-transform" mode="out-in"> <div class="request-loading-component" v-if="requestLoading"> <v-progress-circular :size="50" color="primary" indeterminate></v-progress-circular> </div> </transition> </template> <script> import { mapGetters } from 'vuex' export default { name: 'RequestLoading', computed: { ...mapGetters(['requestLoading']) } } </script> <style lang="stylus" scoped> .request-loading-component { position: fixed; left: 0; right: 0; top: 0; bottom: 0; background-color: rgba(48, 65, 86, 0.5); font-size: 150px; display: flex; flex-direction: row; justify-content: center; align-items: center; z-index: 999999; } </style>
这里咱们用到了,vuetify中的v-progress-circular
接下来咱们配置一下vuex
const app = { state: { requestLoading: 0 }, mutations: { SET_LOADING: (state, status) => { // error 的时候直接重置 if (status === 0) { state.requestLoading = 0 return } if (status) { ++state.requestLoading } else { --state.requestLoading } } }, actions: { SetLoading({ commit }, status) { commit('SET_LOADING', status) } } } export default app
const getters = { requestLoading: (state) => state.app.requestLoading, token: (state) => state.user.token, avatar: (state) => state.user.avatar, name: (state) => state.user.name } export default getters
// 添加请求拦截器 service.interceptors.request.use( (config) => { if (config.method === 'post' || config.method === 'put') { // post、put 提交时,将对象转换为string, 为处理Java后台解析问题 config.data = JSON.stringify(config.data) } // loading + 1 store.dispatch('SetLoading', true) // 请求发送前进行处理 return config }, (error) => { // 请求错误处理 // loading 清 0 setTimeout(function() { store.dispatch('SetLoading', 0) }, 300) return Promise.reject(error) } ) // 添加响应拦截器 service.interceptors.response.use( (response) => { let { data, headers } = response if (headers['x-auth-token']) { data.token = headers['x-auth-token'] } // loading - 1 store.dispatch('SetLoading', false) return data }, (error) => { let info = {}, { status, statusText, data } = error.response if (!error.response) { info = { code: 5000, msg: 'Network Error' } } else { // 此处整理错误信息格式 info = { code: status, data: data, msg: statusText } } // loading - 1 store.dispatch('SetLoading', false) return Promise.reject(info) } )
这样咱们在等待接口返回数据是就会看到下面这样子:
好啦 ,今天就到这里吧,若是有什么疑问,能够下面留言,小肆会及时回复的。记得点好看呦!