<!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 to="/home">主頁</router-link>
<router-link to="/user">使用者</router-link>
<router-link to="/finance">財務資訊</router-link> -->
<!-- 寫成動態的 -->
<!-- $router.options.routes 可以從計算器屬性-->
<!-- {{$router.options.routes}} -->
<router-link v-for="(item,index) in $router.options.routes" :key="index" :to="item.path">{{item.meta.title}}
</router-link>
<!-- <router-link v-for="(item,index) in getMyRoutes" :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>