27.《VUE之点击链接高亮模式》

借助router插件,他提供了一个专门给被选中的router-link绑定样式的类名!

.router-link-avtive

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>VUE之点击链接高亮模式</title>
		<!--引入文件-->
		<script type="text/javascript" src="js/vue.js" ></script>
        <script type="text/javascript" src="js/vue-router.js" ></script>
	</head>
	<style>
		.router-link-active{
			color: blue;
			font-weight: bold;
		}
	</style>
	<body>
		<!--
        	vue-router插件提供的特殊标签,它可以扮演任何标签
        	to:套跳转去哪儿,to属性中的路径不在需要#号
        	tag:说明vue-router要体现为哪种标签
        
        -->
		<div id="app">
			<!--<a href="#/article">推荐</a>
			<a href="#/attention">关注</a>
			<a href="#/Comment">评论</a>-->
			
			<router-link to="/article">推荐</router-link>
			<router-link to="/attention">关注</router-link>
			<router-link to="/Comment">评论</router-link>
			
			<!--定义路由的视图区域-->
			<router-view></router-view>
			
		</div>
		<!--制定路由相应的组件-->
		<template id="temp1">
			<div class="panel">推荐文章</div>
		</template>
		
		<template id="temp2">
			<div class="pane2">我的关注</div>
		</template>
		
		<template id="temp3">
			<div class="pane3">我的评论</div>
		</template>
		
		<script>
			//2.注册组件
			var tempArticle={
				template:'#temp1'
			}
			var tempAttention={
				template:'#temp2'
			}
			var tempComment={
				template:'#temp3'
			}
			
			Vue.component('article',tempArticle);
			Vue.component('attention',tempAttention);
			Vue.component('Comment',tempComment);
			//制定路由规则
			var routeRules=new VueRouter({
				//routes属性用于定义规则 规则不止一条
				//每一个元素就是一个路由规则
				routes:[
				   //3.一条路由规则含有两个信息 路径 组件
				   {path:'/article',component:tempArticle},
				   {path:'/attention',component:tempAttention},
				   {path:'/Comment',component:tempComment},
				   {path:'/',redirect:'/article'}
				]
				
			});
			var  vr=new Vue({
				el:'#app',
				//4.把路由规则注册到vr实例中
				router:routeRules 
			})
		</script>
	</body>
</html>

如果不喜欢这个,可以任意类名定义高亮样式,然后绑定到路由规则的linkActiveClass属性上,具体代码如下:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>VUE之点击链接高亮模式</title>
		<!--引入文件-->
		<script type="text/javascript" src="js/vue.js" ></script>
        <script type="text/javascript" src="js/vue-router.js" ></script>
	</head>
	<style>
		.aaa{
			color: blue;
			font-weight: bold;
		}
		/*.router-link-active{
			color: blue;
			font-weight: bold;
		}*/
	</style>
	<body>
		<!--
        	vue-router插件提供的特殊标签,它可以扮演任何标签
        	to:套跳转去哪儿,to属性中的路径不在需要#号
        	tag:说明vue-router要体现为哪种标签
        
        -->
		<div id="app">
			<!--<a href="#/article">推荐</a>
			<a href="#/attention">关注</a>
			<a href="#/Comment">评论</a>-->
			
			<router-link to="/article">推荐</router-link>
			<router-link to="/attention">关注</router-link>
			<router-link to="/Comment">评论</router-link>
			
			<!--定义路由的视图区域-->
			<router-view></router-view>
			
		</div>
		<!--制定路由相应的组件-->
		<template id="temp1">
			<div class="panel">推荐文章</div>
		</template>
		
		<template id="temp2">
			<div class="pane2">我的关注</div>
		</template>
		
		<template id="temp3">
			<div class="pane3">我的评论</div>
		</template>
		
		<script>
			//2.注册组件
			var tempArticle={
				template:'#temp1'
			}
			var tempAttention={
				template:'#temp2'
			}
			var tempComment={
				template:'#temp3'
			}
			
			Vue.component('article',tempArticle);
			Vue.component('attention',tempAttention);
			Vue.component('Comment',tempComment);
			//制定路由规则
			var routeRules=new VueRouter({
				//routes属性用于定义规则 规则不止一条
				//每一个元素就是一个路由规则
				routes:[
				   //3.一条路由规则含有两个信息 路径 组件
				   {path:'/article',component:tempArticle},
				   {path:'/attention',component:tempAttention},
				   {path:'/Comment',component:tempComment},
				   {path:'/',redirect:'/article'}
				],
				linkActiveClass:'aaa'
				
			});
			var  vr=new Vue({
				el:'#app',
				//4.把路由规则注册到vr实例中
				router:routeRules 
			})
		</script>
	</body>
</html>

谢谢访问!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值