搭建Vue從Vue-cli到router路由護(hù)衛(wèi)的實(shí)現(xiàn)
別的不多說(shuō),開(kāi)始動(dòng)爪把,
首先安裝vue-cli mac: sudo npm install -g @vue/cli
github:
https://github.com/XinYueXiao/vue-routes
1、Vue-cli基礎(chǔ)使用
1.1 創(chuàng)建測(cè)試項(xiàng)目 vue create vue-routes

1.2 創(chuàng)建成功,啟動(dòng)項(xiàng)目 yarn serve
在 http://localhost:8080/ 就可以看到歡迎:clap:頁(yè)面了

1.3 搞點(diǎn)自定義配置,新建vue.config.js
const title = '雙11剁手啦'
const port = '1111'
module.exports = {
publicPath: '/wxy',
//自定義端口號(hào)
devServer: {
port
},
//自定義變量
configureWebpack: {
name: title
}
}
配置完成后重新啟動(dòng) yarn serve 效果圖

如何配置svg圖標(biāo)
1)準(zhǔn)備一個(gè)svg,例如: src/icons/svg/hg.svg
2)安裝loader yarn add svg-sprite-loader
3)對(duì)config進(jìn)行鏈?zhǔn)讲僮骷纯尚薷膌oader
const path = require('path')
//處理地址
function resolve(dir) {
return path.join(__dirname, dir)
}
module.exports = {
...,
chainWebpack(config) {
//安裝loader,對(duì)config進(jìn)行鏈?zhǔn)讲僮骷纯尚薷膌oader、plugins
//1.svg rule中要排除icons目錄
config.module.rule('svg')
//轉(zhuǎn)換為絕對(duì)地址
.exclude.add(resolve('src/icons'))
//查看配置后svg規(guī)則 vue inspect --rule svg
//2.添加一個(gè)規(guī)則icons
config.module.rule('icons')
.test(/\.svg$/)
.include.add(resolve('src/icons')).end()
.use('svg-sprite-loader')
.loader('svg-sprite-loader')
.options({
symbolId: 'icon-[name]'
})
}
}
4)svg rule中要排除icons目錄后配置

5) 添加一個(gè)規(guī)則icons配置

6) 新建 src/components/SvgIcon.vue 模板
<template>
<svg :class="svgClass" aria-hidden="true" v-on="$listeners">
<use :xlink:href="iconName" rel="external nofollow" />
</svg>
</template>
<script>
export default {
name: "SvgIcon",
props: {
iconClass: {
type: String,
required: true
},
className: {
type: String,
default: ""
}
},
computed: {
iconName() {
return `#icon-${this.iconClass}`;
},
svgClass() {
if (this.className) {
return "svg-icon " + this.className;
} else {
return "svg-icon";
}
}
}
};
</script>
<style scoped>
.svg-icon {
width: 1em;
height: 1em;
vertical-align: -0.15em;
fill: currentColor;
overflow: hidden;
}
</style>
7)新建 src/icons/index.js 在main.js下引入icon
//src/icons/index.js
import Vue from 'vue'
import SvgIcon from '@/components/SvgIcon'
//圖標(biāo)自動(dòng)加載
const req = require.context('./svg', false, /\.svg$/)
req.keys().map(req)
Vue.component('svg-icon', SvgIcon)
//main.js
import "./icons";
8)在App.vue引入圖標(biāo)
<svg-icon icon-class="hg"></svg-icon>
效果如下:

2、router路由守衛(wèi)
何為守衛(wèi),即為阻止無(wú)身份者進(jìn)入組織內(nèi)部
安裝yarn add vue-router 控制路由
安裝yarn add vuex 存儲(chǔ)身份認(rèn)證

2.1 路由配置
src/router/index.js
import Vue from "vue";
import Router from "vue-router";
import Layout from '@/layout'; // 布局頁(yè)
Vue.use(Router);
// 通用頁(yè)面:不需要守衛(wèi),可直接訪問(wèn)
export const constRoutes = [
{
path: "/login",
component: () => import("@/views/Login"),
hidden: true // 導(dǎo)航菜單忽略該項(xiàng)
}, {
path: "/",
component: Layout,// 應(yīng)用布局
redirect: "/home",
children: [
{
path: "home",
component: () =>
import(/* webpackChunkName: "home" */ "@/views/Home.vue"),
name: "home",
meta: {
title: "Home", // 導(dǎo)航菜單項(xiàng)標(biāo)題
icon: "hg" // 導(dǎo)航菜單項(xiàng)圖標(biāo)
}
}]
}];
// 權(quán)限頁(yè)面:受保護(hù)頁(yè)面,要求用戶登錄并擁有訪問(wèn)權(quán)限的角色才能訪問(wèn)
export const asyncRoutes = [
{
path: "/about",
component: Layout,
redirect: "/about/index",
children: [
{
path: "index",
component: () =>
import(/* webpackChunkName: "home" */ "@/views/About.vue"),
name: "about",
meta: {
title: "About",
icon: "hg",
roles: ['admin', 'editor']
},
}
]
}
];
export default new Router({
mode: "history",
base: process.env.BASE_URL,
routes: constRoutes
});
布局組件 src/layout
<template> <div class="app-wrapper"> <div class="main-container"> <router-view /> </div> </div> </template>
路由展示src/App.vue
<template>
<div id="app">
<!-- 路由 -->
<div id="nav">
<router-link to="/">
<svg-icon icon-class="wx"></svg-icon>
<!-- <svg>
<use xlink:href="#icon-wx" rel="external nofollow" ></use>
</svg>-->
Home
</router-link>|
<router-link to="/about">
<svg-icon icon-class="hg"></svg-icon>About
</router-link>
</div>
<!-- 4.路由視圖 -->
<!-- 問(wèn)題:router-link和router-view是哪來(lái)的 -->
<router-view></router-view>
</div>
</template>
<script>
export default {
name: "app",
components: {}
};
</script>
<style>
#app {
font-family: "Avenir", Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
2.2 準(zhǔn)備頁(yè)面
src/views/About.vue
<template> <div class="about"> <h1>This is an about page</h1> </div> </template>
src/views/Home.vue
<template>
<div class="home">
<img alt="Vue logo" src="../assets/logo.png" />
<HelloWorld msg="Welcome to Your Vue.js App" />
</div>
</template>
<script>
// @ is an alias to /src
import HelloWorld from "@/components/HelloWorld.vue";
export default {
name: "home",
components: {
HelloWorld
}
};
</script>
src/views/Login.vue
<template>
<div>
<h2>用戶登錄</h2>
<div>
<input type="text" v-model="username" />
<button @click="login">登錄</button>
</div>
</div>
</template>
<script>
export default {
data() {
return {
username: "admin"
};
},
methods: {
login() {
this.$store
.dispatch("user/login", { username: this.username })
.then(() => {
this.$router.push({
path: this.$route.query.redirect || "/"
});
})
.catch(error => {
alert(error);
});
}
}
};
</script>
2.3 身份認(rèn)證
import router from "./router";
import store from "./store";
const whiteList = ["/home", "/login"]; // 無(wú)需令牌白名單
// 全局路由守衛(wèi)
router.beforeEach(async (to, from, next) => {
// 獲取令牌判斷用戶是否登錄
const hasToken = localStorage.getItem("token");
// 已登錄
if (hasToken) {
if (to.path === "/login") {
// 若已登錄沒(méi)有必要顯示登錄頁(yè),重定向至首頁(yè)
next({ path: "/" });
} else {
// 去其他路由,暫時(shí)放過(guò)
// next()
// 接下來(lái)執(zhí)行用戶角色邏輯, todo
// 1.判斷用戶是否擁有角色
const hasRoles =
store.state.user.roles && store.state.user.roles.length > 0;
if (hasRoles) {
next();
} else {
// 2.獲取用戶角色
const roles = await store.dispatch("user/getInfo");
const accessRoutes = await store.dispatch("permission/generateRoutes", roles);
// 動(dòng)態(tài)添加路由到路由器
router.addRoutes(accessRoutes);
// 跳轉(zhuǎn)
next({ ...to });
}
}
} else {
// 未登錄
if (whiteList.indexOf(to.path) !== -1) {
// 白名單中路由放過(guò)
next();
} else {
// 重定向至登錄頁(yè)
next(`/login?redirect=${to.path}`);
}
}
});
2.4 用戶信息設(shè)置
import Vue from "vue";
import Vuex from "vuex";
import user from './modules/user'
import permission from './modules/permission'
Vue.use(Vuex);
export default new Vuex.Store({
modules: {
user, permission
}
});
src/store/modules/user.js
const state = {
token: localStorage.getItem("token"),
// 其他用戶信息
roles: []
};
const mutations = {
SET_TOKEN: (state, token) => {
state.token = token;
},
SET_ROLES: (state, roles) => {
state.roles = roles;
},
};
const actions = {
// 模擬用戶登錄
login({ commit }, userInfo) {
const { username } = userInfo;
return new Promise((resolve, reject) => {
setTimeout(() => {
if (username === "admin" || username === "jerry") {
commit("SET_TOKEN", username);
localStorage.setItem("token", username);
resolve();
} else {
reject("用戶名、密碼錯(cuò)誤");
}
}, 1000);
});
},
getInfo({ commit, state }) {
return new Promise((resolve) => {
setTimeout(() => {
const roles = state.token === 'admin' ? ['admin'] : ['editor']
commit('SET_ROLES', roles)
resolve(roles)
}, 1000);
})
}
};
export default {
namespaced: true,
state,
mutations,
actions,
};
2.5 用戶路由權(quán)限 src/store/modules/permission.js
// 導(dǎo)入asyncRoutes,過(guò)濾它看當(dāng)前用戶是否擁有響應(yīng)權(quán)限
import {asyncRoutes, constRoutes} from '@/router'
const state = {
routes: [], // 完整路由
addRoutes: [], // 權(quán)限路由
}
const mutations = {
// routes: 用戶可訪問(wèn)的權(quán)限路由
SET_ROUTES: (state, routes) => {
state.addRoutes = routes;
state.routes = constRoutes.concat(routes);
}
}
const actions = {
generateRoutes({commit}, roles) {
// 過(guò)濾出能訪問(wèn)的路由表
const routes = filterAsyncRoutes(asyncRoutes, roles)
commit('SET_ROUTES', routes)
return routes;
}
}
function filterAsyncRoutes(routes, roles) {
const res = [];
routes.forEach(route => {
// 復(fù)制一份路由
const tmp = {...route};
// 擁有訪問(wèn)權(quán)限
if (hasPermission(roles, tmp)) {
if (tmp.children) {
// 遞歸子路由
tmp.children = filterAsyncRoutes(tmp.children, roles)
}
res.push(tmp);
}
})
return res;
}
function hasPermission(roles, route) {
if (route.meta && route.meta.roles) {
return roles.some(role => route.meta.roles.includes(role))
} else {
// 路由定義中沒(méi)有roles選項(xiàng),則不需要權(quán)限即可訪問(wèn)
return true;
}
}
export default {
namespaced: true,
state,
mutations,
actions
}
2.6 最終效果圖

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 在vue-cli腳手架中配置一個(gè)vue-router前端路由
- VUE : vue-cli中去掉路由中的井號(hào)#操作
- Vue-CLI項(xiàng)目中路由傳參的方式詳解
- 基于vue-cli 路由 實(shí)現(xiàn)類似tab切換效果(vue 2.0)
- vue-cli 默認(rèn)路由再子路由選中下的選中狀態(tài)問(wèn)題及解決代碼
- vue-cli實(shí)現(xiàn)多頁(yè)面多路由的示例代碼
- 詳解在vue-cli中使用路由
- 詳解windows下vue-cli及webpack 構(gòu)建網(wǎng)站(四) 路由vue-router的使用
- 深入理解Vue-cli4路由配置
相關(guān)文章
vue導(dǎo)入.md文件的步驟(markdown轉(zhuǎn)HTML)
這篇文章主要介紹了vue導(dǎo)入.md文件的步驟(markdown轉(zhuǎn)HTML),幫助大家更好的理解和使用vue框架,感興趣的朋友可以了解下2020-12-12
解決vue項(xiàng)目打包上服務(wù)器顯示404錯(cuò)誤,本地沒(méi)出錯(cuò)的問(wèn)題
這篇文章主要介紹了解決vue項(xiàng)目打包上服務(wù)器顯示404錯(cuò)誤,本地沒(méi)出錯(cuò)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11
vue :style設(shè)置背景圖片方式backgroundImage
這篇文章主要介紹了vue :style設(shè)置背景圖片方式backgroundImage,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
Vue el使用el-checkbox-group復(fù)選框進(jìn)行單選框方式
這篇文章主要介紹了Vue el使用el-checkbox-group復(fù)選框進(jìn)行單選框方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
解決vue中post方式提交數(shù)據(jù)后臺(tái)無(wú)法接收的問(wèn)題
今天小編就為大家分享一篇解決vue中post方式提交數(shù)據(jù)后臺(tái)無(wú)法接收的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
vue項(xiàng)目中如何實(shí)現(xiàn)網(wǎng)頁(yè)的截圖功能?(html2canvas)
這篇文章主要介紹了vue項(xiàng)目中如何實(shí)現(xiàn)網(wǎng)頁(yè)的截圖功能?(html2canvas),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02

