當我們點擊 展示相應組建, 標籤會加個class 屬性,如圖,
如果想高亮就需要 重寫這個樣式
<style>
.router-link-active{
background-color: green;
font-size: larger;
color: aliceblue
}
</style>
我們也可以自定義一下, 在 router實例對象添加
myactive: 'router-link-active',
也可以實現
全部代碼如下:
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
<title>Page Title</title>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<script src='../../lib/vue-2.4.0.js'></script>
<script src='../../vue-router-3.0.1.js'></script>
<style>
.router-link-active{
background-color: green;
font-size: larger;
color: aliceblue
}
</style>
</head>
<body>
<div id="pp">
<!-- 創建兩個button, 並通過href 找到相應組建的路由 -->
<router-link to='/login' tag="span">登錄</router-link>
<router-link to='/registe' tag="span">註冊</router-link>
<!-- 用於顯示路由組建 -->
<router-view></router-view>
</div>
<script>
// 創建兩個組建模板對象,讓路由能找到並顯示這個組件
var tmp1={
template:'<h1>登錄</h1>',
}
var tmp2={
template:'<h1>註冊</h1>',
}
// 創建路由對象,並設置路由的參數 ,第一個是path, 第二個是 要匹配的組建
var routerObj=new VueRouter({
routes:[
{path:'/',redirect:'/login'},
{path:'/login',component:tmp1},
{path:'/registe',component:tmp2}
],
myactive: 'router-link-active',
})
new Vue({
el:'#pp',
data:{
},
methods:{
},
// 讓路由對象 和vue 實例 綁定關係, 用於監聽。
router: routerObj,
})
</script>
</body>
</html>