I have a vue3 router defined with the following routes
export const routes: Array<RouteRecordRaw> = [
{
path: "/",
name: "浏览题目",
component: HomeView,
meta: {
access: "canAdmin",
},
},
{
path: "/noAuth",
name: "无权限",
component: NoAuthView,
meta: {
access: "canAdmin",
},
},
{
path: "/admin",
name: "管理员可见",
component: AdminView,
meta: {
access: "canAdmin",
},
},
{
path: "/about",
name: "关于我的",
// 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/AboutView.vue"),
},
To implement a simple permission management function, determine whether the user has access permission based on the routing information of the page accessed by the user. If so, it will jump to the original page, otherwise it will be intercepted.
<script setup lang="ts">
import BasicLayout from "@/layouts/BasicLayout.vue";
import { useRouter } from "vue-router";
import { useStore } from "vuex";
const router = useRouter();
const store = useStore();
router.beforeEach((to, from, next) => {
if (to.meta?.access === "canAdmin") {
if (store.state.user.loginUser?.role !== "admin") {
next("/noAuth");
return;
}
}
next();
});
</script>
Why do I get an error when I click to jump to another page after refreshing?