搭建Vue从Vue-cli到router路由护卫的实现

2020-06-12 20:49:06易采站长站整理

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.路由视图 -->
<!-- 问题:router-link和router-view是哪来的 -->
<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  准备页面

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>