<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>权限控制- filters</title>
<style>
.active {
font-size: 20px;
color: #ff7300;
text-decoration: none;
}
.main-menu a {
display: inline-block;
margin-right: 10px;
}
</style>
<script src="js/vue.js"></script>
<script src="js/vue-router.js"></script>
</head>
<body>
<div id="itapp">
<div class="main-menu">
<router-link v-for="(item,index) in $router.options.routes" :key="index" :to="item.path">{{item.meta.title}}
</router-link>
</div>
<div>
<transition enter-active-class="animated bounceInLeft" leave-active-class="animated bounceOutRight">
<router-view></router-view>
</transition>
</div>
<hr>
<button @click="push">添加路由</button>
<button @click="replace">替换路由</button>
</div>
<template id="user">
<div>
<h3>用户信息</h3>
<ul>
<router-link to="/user/login?name=tom&pwd=123" tag="li">用户登陆</router-link>
<router-link to="/user/regist/alice/456" tag="li">用户注册</router-link>
<router-link to="/user/info" tag="li">用户注册</router-link>
</ul>
<router-view></router-view>
</div>
</template>
<script>
var Home = {
template: '<h3>我是主页</h3>'
}
var User = {
template: '#user'
}
var Login = {
template: '<h4>用户登陆。。。获取参数:{{$route.query}},{{$route.path}}</h4>'
}
var Regist = {
template: '<h4>用户注册。。。获取参数:{{$route.params}},{{$route.path}}</h4>'
}
var Finance = {
template: '<h4>财务信息</h4>'
}
var routes = [{
path: '/home',
component: Home,
meta: {
title: '首页',
roles: ['admin', 'guest']
}
},
{
path: '/user',
component: User,
meta: {
title: '用户',
roles: ['admin', 'guest']
},
children: [{
path: 'login',
component: Login
},
{
path: 'regist/:username/:password',
component: Regist
}
]
},
{
path: '/finance',
component: Finance,
meta: {
title: '财务信息',
roles: ['admin']
}
},
{
path: '*',
redirect: '/home',
hidden: true
}
]
routes = routes.filter((option, index) => {
return !option.hidden && (option.meta && option.meta.roles.includes('guest'));
})
const routerAll = new VueRouter({
routes,
linkActiveClass: 'active',
linkExactActiveClass: 'active-extact',
mode: "hash",
});
new Vue({
el: '#itapp',
router: routerAll,
computed: {
getMyRoutes() {
var thisData = this.$router.options.routes;
var test1 = thisData.filter((option) => {
return option.meta
})
return test1;
}
},
methods: {
push() {
this.$router.push({
path: 'home'
});
},
replace() {
routerAll.replace({
path: 'user'
});
}
}
});
</script>
</body>
</html>