vue使用vuex实现首页导航切换不同路由的方法
程序员文章站
2023-12-12 22:27:16
vue实现首页导航切换不同路由的方式(二)【使用vuex实现的】,具体代码如下所示:
vue实现首页导航切换不同路由的方式(二)【使用vuex实现的】,具体代码如下所示:
<nav> <!-- 导航栏 --> <div class="indexnavout"> <div class="indexnav"> <ul class="navli"> <li @click="checknav()" style="width: 130px;"><router-link to="/home">了解海华教育(视频)</router-link><!--<a href="#">了解海华教育(视频)</a>--></li> <li @click="checknav()" v-bind:class="{'active':index=='#/home','active2':index=='#/'}"> <router-link to="/home">首页</router-link> </li> <li @click="checknav()" v-bind:class="{'active':index=='#/synopsis'}"><router-link to="/synopsis">学校简介</router-link> </li> <li @click="checknav()" v-bind:class="{'active':index=='#/major'}"><router-link to="/major">专业介绍</router-link> </li> <li @click="checknav()" v-bind:class="{'active':index=='#/campus'}"><router-link to="/campus">校园风采</router-link> </li> <li @click="checknav()" v-bind:class="{'active':index=='#/news'}"><router-link to="/news">新闻资讯</router-link> </li> <li @click="checknav()" v-bind:class="{'active':index=='#/enrollment'}"><router-link to="/enrollment">招生信息</router-link> </li> <li @click="checknav()" v-bind:class="{'active':index=='#/employment'}"><router-link to="/employment">就业中心</router-link> </li> <li @click="checknav()" v-bind:class="{'active':index=='#/enlist'}"><router-link to="/enlist">在线报名</router-link> </li> <li @click="checknav()" v-bind:class="{'active':index=='#/contact'}"><router-link to="/contact">联系我们</router-link> </li> </ul> </div> </div> </nav>
css:
/* 导航栏 */ .indexnavout{ background-color: #486b8a; height: 60px; } .indexnav{ width: 80%; margin: 0 auto; } .navli{ display: flex; } .navli li:nth-child(1){ flex: 1.8; } .navli li{ flex: 1; } .navli li a{ text-decoration: none; color: #fff; font-size: 16px; padding-bottom: 17px; } .indexnav .icon{ width: 230px; margin-top: 10px; } .indexnav .icon img{ width: 100%; } .navli li { height: 60px; line-height: 60px; position: relative; } .navli li.active a{ border-bottom: 3px solid #e96463; } .navli li.active2 a{ border-bottom: 3px solid #e96463; } .navli li:last-child:after{ border-right: none; } .navli li .line{ width: 20px; height: 3px; background: #e96463; border: none; position: absolute; /*top: -14px;*/ /*right: -32px;*/ left: 50px; top: 45px; z-index: 100; }
js:
main.js代码:
import vuex from 'vuex' vue.use(vuex); const store = new vuex.store({ state: { majordetail: false, index: document.location.hash,//导航条跳转路由样式控制 }, mutations: { //控制专业介绍详情显示隐藏 updatemajordetail(state, majordetail) { state.majordetail = majordetail; }, updateindex(state, index) { state.index = index; } } });
效果图:
现在在这个页面里点击了解更多的时候跳转到新闻资讯页面,然后然后state里的值,然后改变当前的url以及导航选中新闻资讯。
总结
以上所述是小编给大家介绍的vue使用vuex实现首页导航切换不同路由的方法,希望对大家有所帮助