vue3.0 +element plus 实现页面刷新 菜单栏保持选中状态

Router

import {
  createRouter,
  createWebHashHistory
} from "vue-router";
import Index from "../views/index/index.vue";
import Login from "../views/login/login.vue"
import Default from "../components/default.vue";
import Adviser from "../components/adviser.vue";
import NotFound from "../components/404.vue";
import User from "../components/user.vue";
import Competence from "../components/competence.vue";


const routes = [{
    path: "/index",
    name: "Index",
    component: Index,
    children: [{
      path: '/user',
      component: User
    }, {
      path: '/home',
      component: Default
    }, {
      path: '/adviser',
      component: Adviser
    }, {
      path: '/notFound',
      component: NotFound
    }, {
      path: '/competence',
      component: Competence
    }]
  },
  {
    path: "/",
    name: "login",
    component: Login,
  },
  {
    path: "/about",
    name: "About",
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () =>
      import( /* webpackChunkName: "about" */ "../views/About.vue"),
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

router.beforeEach((to, from, next) => {

  if (to.path === '/') {
    $.ajax({
      url: "/user/logout",
      type: "post",
      dataType: "json",
      timeout: 2000,
      success: function (data) {
        sessionStorage.removeItem("competenceUrl");
        router.replace('/');
      },
      //调用执行后调用的函数 请求完成后最终执行参数
      complete: function (XMLHttpRequest, textStatus) {
        //超时,status还有success,error等值的情况
        if (textStatus == 'timeout') {
          next('/');
        }
      }
    });
  }
  next();
})

export default router;

首页核心代码

 :default-active="activeIndex"  当前激活菜单的index  
setCurrentRoute() {
  this.activeIndex = this.$route.path; //关键   通过他就可以监听到当前路由状态并激活当前菜单
}

假设 activeIndex=‘/index’el-menu-item :index 也等于/index时选中

<el-menu-item
                v-for="competence in menu.competenceList"
                :index="competence.competenceUrl" 
                :key="menu.id + '-' + competence.id"
              >
</el-menu-item>

**

<el-aside class="menubar">
          <el-menu
            router
            :default-active="activeIndex" 
            class="el-menu-vertical-demo"
            :unique-opened="true"
            @select="handleSelect"
          >
            <el-submenu
              v-for="menu in menuList"
              :index="menu.id + ''"
              :key="menu.id"
            >
              <template #title>
                <i
                  :class="
                    menu.iconValue == null ? 'el-icon-link' : menu.iconValue
                  "
                ></i>
                <span>{{ menu.menuName }}</span>
              </template>
              <el-menu-item
                v-for="competence in menu.competenceList"
                :index="competence.competenceUrl"
                :key="menu.id + '-' + competence.id"
              >
                <i class="el-icon-connection"></i>
                <template #title>{{ competence.competenceName }}</template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>

export default {
  data() {
    return {
      activeIndex: "/home",
      logo: require("../../assets/logo.png"),
      handlerUser: {
        id: 0,
        username: "",
        userBase: {
          img: "",
          nickname: "",
        },
      },
      menuList: [],
      competenceList: [],
    };
  },
  watch() {
    this.setCurrentRoute();
  },
  methods: {
    setCurrentRoute() {
      this.activeIndex = this.$route.path; //关键   通过他就可以监听到当前路由状态并激活当前菜单
    }
    handleSelect(data) {
      this.$router.replace({ path: data });
    },
  }
};
</script>
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 我很抱歉,我不能给你具体的 Vue3 Element-plus 编写的博客页面代码,因为这需要具体的需求和实现细节。我可以提供一些帮助和指导来解决你的问题。 建议你首先了解 Vue3 的基础知识和 Element-plus 的使用方法。然后,你可以使用 Vue CLI 创建一个新的 Vue3 目,并在目中安装 Element-plus。 你可以定义一些组件来实现博客页面的不同部分,例如顶部导航栏、博客列表、博客内容等。你可以使用 Element-plus 的组件来帮助你实现这些部分。 最后, 你可以使用 Vue3 的路由功能来实现博客页面的导航和跳转。 希望这些提示能帮助你编写博客页面。如果你有其他问题,请随时问我。 ### 回答2: Vue3和Element Plus是两个独立的目,Vue3是Vue.js框架的下一个主要版本,而Element Plus是一个基于Vue3的UI组件库。因此,编写博客页面的代码使用Vue3和Element Plus来实现。 首先,我们需要安装Vue3和Element Plus。可以通过npm或yarn来安装它们: ``` npm install vue@next npm install element-plus ``` 在Vue3中,我们需要创建一个Vue实例,并在html文件中引用: ```html <!DOCTYPE html> <html> <head> <title>博客页面</title> </head> <body> <div id="app"></div> <script src="https://unpkg.com/vue@next"></script> <script src="https://unpkg.com/element-plus"></script> <script src="app.js"></script> </body> </html> ``` 然后,在app.js文件中编写Vue3和Element Plus的代码: ```javascript import { createApp } from 'vue'; import ElementPlus from 'element-plus'; import 'element-plus/lib/theme-chalk/index.css'; const app = createApp(); app.use(ElementPlus); app.mount('#app'); ``` 上面的代码中,我们通过import语句引入了Vue3的createApp方法和Element Plus组件库,并使用app.use方法来告诉Vue使用Element Plus。然后,使用app.mount方法将Vue实例挂载到id为app的div上。 在编写博客页面的具体内容时,可以使用Element Plus提供的各种组件来实现页面的布局和样式。例如,可以使用Layout组件来创建页面的整体布局,使用Menu组件来创建导航菜单,使用Button组件来添加按钮等等。 以上是关于使用Vue3和Element Plus编写博客页面的基本步骤和示例代码。具体的实现还需要根据具体需求进行调整和编写。 ### 回答3: Vue3是一种用于构建用户界面的开源JavaScript框架,而Element Plus是一个基于Vue3的组件库。如果要编写一个博客页面,可以使用Vue3和Element Plus来创建页面的结构和组件。 首先,需要在Vue3的目中安装和引入Element Plus。可以通过npm或yarn来安装依赖,并在主文件中引入所需的组件。 在页面的模板部分,可以使用Element Plus提供的布局组件来创建页面的整体结构。例如,可以使用Layout组件来定义头部、侧边栏和内容区域的布局。在内容区域中,可以使用Container组件来划分为不同的区块。 除了布局组件外,Element Plus还提供了丰富的表单组件、按钮组件、图片组件等等,可以根据博客页面的需求选择合适的组件进行使用。 在Vue3的脚本部分,可以使用Vue3的响应式数据和计算属性来处理页面状态和逻辑。例如,可以使用ref来定义响应式数据,使用computed来定义计算属性。 另外,Vue3还引入了Composition API,可以使用setup函数来组合页面的逻辑。可以在setup函数中使用reactive将响应式数据转换为可响应式对象,使用watchEffect来监听响应式数据的变化。 在博客页面中,还可以使用Vue3的路由功能来实现页面之间的跳转。可以使用Vue Router来配置路由表,并结合Element Plus的导航栏组件来生成页面导航。 总之,使用Vue3和Element Plus可以轻松编写出具有丰富功能和良好用户体验的博客页面。通过合理使用Element Plus的组件和Vue3的特性,可以提高开发效率和代码可维护性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值