vue 头部导航样式的设置(watch监听)

一开始用的 :index 动态改变index值来激活每个li的样式,头部切换效果可以实现

	<el-menu-item  :index="indexF">
		<router-link to="/homePage">
			<div >首页</div>
		</router-link>
	</el-menu-item>
	<el-menu-item  :index="indexS">
		<router-link to="/translateNowPage">
			<div >首页</div>
		</router-link>
	</el-menu-item>	

在这里插入图片描述
但,登录之后,要求立即翻译处在激活状态,退出系统,要求首页处在激活状态,账号设置,我的处在在激活状态。这需要对每次的状态值进行保存并获取,我存在vuex中,有时会出现首页和立即翻译同时选中,等等其他多个li同时选中,实现起来比较麻烦。因此换了一种实现方式。

摒弃之前的用index实现激活状态的思路,通过点击事件动态给每个li设置样式,从而实现切换导航和登录
时li处在激活状态

	<el-menu-item  :class="activeClass ==1?'active':''" @click="navClick(1)">
		<router-link to="/homePage">
			<div >首页</div>
		</router-link>
	</el-menu-item>
	<el-menu-item  :class="activeClass ==2?'active':''" @click="navClick(2)">
		<router-link to="/translateNowPage">
			<div>立即翻译</div>
		</router-link>
	</el-menu-item>
	data() {
		return {
			activeClass:1
		}
	},
	methods:{
		//头部导航切换,激活样式
		navClick(m){
			this.activeclass = m;
		},	
	}
	.active {
		color: rgb(255, 255, 255)!important; 
		border-bottom-color: rgb(255, 255, 255)!important;
		background-color: rgb(0, 102, 255)!important;
	 }

此时,又出现一个新的问题
在这里插入图片描述
需求:当点击立即翻译时,立即翻译激活。但是,因为头部导航和下边内容是子父级路由关系,子级路由传值到vuex中,因为父级页面一直打开而无法触发created方法。所以我在父级页面用watch监听子级路由变化并判断触发条件,从而实现功能

	//账号设置点击事件
	accountSetting(){
		this.$router.push({path:'/myPoints', query: {key: 'account'}})
		this.indexStatus= 1;		//账号设置的状态
	},

	getIndex() {
		if(this.indexStatus== 1){				//账号设置
			this.activeclass = 4;
			this.indexStatus= ''	
		}else if(this.indexStatus== 2){		//退出系统
			_this.activeclass = 1;
		}else{
			this.activeclass = 2;		//立即翻译
		}
	},
	watch: {
		// 如果路由有变化,会执行该方法
		"$route": "getIndex"
	}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值