Vue项目权限控制

Vue项目权限控制

html
<script setup lang="ts">
  import { provide, ref, watch } from "vue"
  import router, { routes } from "./router"
  const showRoutes = ref(routes)
  provide("showRoutes", showRoutes)
  const logout = () => {
    location.href = "/"
  }
  watch(showRoutes, () => {
    console.log(router.getRoutes()) // 查看当前路由列表
  })
</script>

<template>
  <router-link v-for="route in showRoutes" :key="route.name" :to="{ name: route.name }">
    【{{ route.meta?.label }}】
  </router-link>
  <button @click="logout">退出登录</button>
  <hr />
  <router-view></router-view>
</template>

<style scoped></style>
html
<script lang="ts" setup>
  import { inject, Ref } from "vue"
  import { loopRoutes, routes } from "../router"

  const showRoutes = inject("showRoutes") as unknown as Ref
  const login = (userType: string) => {
    showRoutes.value = loopRoutes(routes, userType)
  }
</script>

<template>
  login.vue
  <button @click="login('user')">用户登录</button>
  <button @click="login('admin')">admin登录</button>
</template>

<style scoped lang="scss"></style>
给vitepress添加algolia搜索
rest client插件使用