vue3项目中的常用插件
1、路由组件
npm install vue-router@4
路由配置
import { createWebHashHistory, createRouter } from "vue-router" ;
import Admin from "@/pages/layout/admin.vue" ;
import Index from "@/pages/index/index.vue" ;
import NotFound from "@/pages/error/404.vue" ;
import Login from "@/pages/login/index.vue" const routes = [ { path: '/' , component: Admin, children: [ { path: "/" , component: Index, meta: { title: '后台首页' } } , ] } , { path: '/login' , component: Login, meta: { title: '登录' } } , { path: "/:pathMatch(.*)*" , name: "NotFound" , component: NotFound, } ,
] ; const router = createRouter ( { history: createWebHashHistory ( ) , routes,
} ) ; export { router}
路由守卫
import { getCookie } from "@/utils" ;
import { router } from "@/router" ;
import { store } from "@/store" ;
import { showLoading, hiddenLoading } from "@/utils" ;
router. beforeEach ( async ( to, from , next ) => { showLoading ( ) const token = getCookie ( "token" ) ; if ( ! token && to. path != "/login" ) { return next ( { path: "/login" } ) ; } if ( token && to. path == "/login" ) { return next ( { path: from . path ? from . path : "/" , } ) ; } if ( token) { await store. dispatch ( 'getUserInfo' ) } let title = ( to. meta. title ? to. meta. title : "" ) + "-这里是标题" document. title = titlenext ( ) ; } ) ;
router. afterEach ( ( to, from ) => { hiddenLoading ( )
} )
2、vuex 用来全局状态共享
npm install vuex@next --save
vuex的配置
import { createStore } from "vuex" ;
import { getInfo } from "@/api/manager" ;
export const store = createStore ( { state: { userInfo: { } , } , mutations: { setUserInfo ( state, userInfo ) { state. userInfo = userInfo; } , } , actions: { getUserInfo ( { commit} ) { return new Promise ( ( resolve, reject ) => { getInfo ( ) . then ( res => { commit ( 'setUserInfo' , res. data) resolve ( res) } ) . catch ( error => reject ( error) ) } ) } , logout ( { commit} ) { commit ( 'setUserInfo' , { } ) } }
} ) ;
3、vuex的数据持久化
npm i vuex-persistedstate
配置
import { createStore } from "vuex"
...
import createPersistedstate from "vuex-persistedstate"
const store = createStore ( { modules: { ... ... } ,plugins: [ createPersistedstate ( { key: '' , paths: [ "demo1" , "demo2" , ] } ) ]
)
4、使用cookie
npm i @vueuse/core
npm i @vueuse/integrations
npm i universal-cookie
使用
import { useCookie} from "@vueuse/integrations/useCookies" const cookie = useCookies ( )
5、https请求 axios
npm install axios --save
axios配置
import axios from "axios" const service = axios. create ( { baseUrl: '' , timeoute: 2000 , ...
} )
service. request. use ( function ( config ) { const token = '' if ( token) { config. headers. token = token} return config; } , function ( error ) { return Promise. reject ( error) ; } ) ;
service. response. use ( function ( response ) { return response; } , function ( error ) { const status = error. reponse. statusif ( status == 401 ) { router. push ( '/login' ) } else if ( status == ... ) { ... . } else { ... . } return Promise. reject ( error) ; } ) ;
6. loading插件
npm install nprogress
nprogress 配置
import nprogress from "nprogress"
nprogress. start ( )
nprogress. done ( )
其余插件为UI库,代码规范,代码检查,CSS预处理器等