element框架导航菜单在页面刷新时会回到默认项

<template>
  <div>
    <el-menu
        :default-active="activeIndex"
        router
        class="el-menu-vertical-demo"
        @select="handleSelect"
        background-color="#545c64"
        text-color="#fff"
        active-text-color="#ffd04b">
        <el-menu-item index="/index/home">
            <i class="el-icon-menu"></i>
            <span slot="title">首页</span>
        </el-menu-item>
        <el-menu-item index="/index/simulationTime">
            <i class="el-icon-time"></i>
            <span slot="title">aaaaaa</span>
        </el-menu-item>
        <!--<el-menu-item index="/index/deductionControl">
            <i class="el-icon-setting"></i>
            <span slot="title">bbbbbb</span>
        </el-menu-item>-->
        <el-menu-item index="/index/simulationProcess">
            <i class="el-icon-tickets"></i>
            <span slot="title">cccccc</span>
        </el-menu-item>
        <el-menu-item index="/index/pilotInterface">
            <i class="el-icon-setting"></i>
            <span slot="title">dddddd</span>
        </el-menu-item>
     </el-menu>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 根据当前路由设置默认值     根据需求使用path或者name
      activeIndex: this.$route.path
    }
  },
  // 通过监听器解决  导航菜单在页面刷新时会回到默认项
  watch: { 
    '$route' (to, from) {
      // 监听路由的变化  如果路由发生改变则当前tab栏默认值也相应改变
      this.activeIndex = to.path
    }
  },
  methods: {
    handleSelect (key, keyPath) {
      this.activeIndex = key
    }
  }
}
</script>
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Vue 框架中,我们可以使用 Element UI 组件库中的 `el-menu` 和 `el-menu-item` 组件来实现导航菜单。同时,我们可以使用 Vue Router 来实现路由跳转。 以下是一个基本的例子: ```html <template> <div> <el-menu default-active="1" class="el-menu-vertical-demo" @select="handleSelect"> <el-menu-item index="1"> <router-link to="/home">首页</router-link> </el-menu-item> <el-menu-item index="2"> <router-link to="/about">关于我们</router-link> </el-menu-item> <el-menu-item index="3"> <router-link to="/contact">联系我们</router-link> </el-menu-item> </el-menu> <router-view></router-view> </div> </template> <script> export default { methods: { handleSelect(index) { console.log(index); }, }, }; </script> ``` 在上面的例子中,我们先使用 `el-menu` 组件创建了一个垂直方向的菜单,并设置了默认选中的菜单项为第一个。然后,我们使用 `el-menu-item` 组件创建了三个菜单项,并在每个菜单项中使用 `router-link` 组件来设置路由跳转的目标路径。 最后,我们使用 `router-view` 组件来渲染实际的页面内容。 在 Vue Router 中,我们需要在路由配置中设置每个路由对应的组件,例如: ```javascript import Vue from "vue"; import VueRouter from "vue-router"; import Home from "@/views/Home.vue"; import About from "@/views/About.vue"; import Contact from "@/views/Contact.vue"; Vue.use(VueRouter); const routes = [ { path: "/", redirect: "/home", }, { path: "/home", component: Home, }, { path: "/about", component: About, }, { path: "/contact", component: Contact, }, ]; const router = new VueRouter({ mode: "history", base: process.env.BASE_URL, routes, }); export default router; ``` 在路由配置中,我们首先设置了根路径 `/` 的重定向到 `/home`,然后分别设置了 `/home`、`/about` 和 `/contact` 路径对应的组件。 在实际开发中,我们可以根据需要进一步定制菜单的样式和行为,例如添加二级菜单、设置菜单的图标等。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值