My Vue3 router is set up with the following routes:
export const routes: Array<RouteRecordRaw> = [
{
path: "/",
name: "Browse Questions",
component: HomeView,
meta: {
access: "canAdmin",
},
},
{
path: "/noAuth",
name: "No Authorization",
component: NoAuthView,
meta: {
access: "canAdmin",
},
},
{
path: "/admin",
name: "Visible to Admin Only",
component: AdminView,
meta: {
access: "canAdmin",
},
},
{
path: "/about",
name: "About Me",
// 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"),
},
In order to implement a basic permission management system, I need to check if the user has permission to access certain pages. If they do, they will be able to proceed to the page, otherwise, they will be redirected.
<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 am I encountering an error when trying to navigate to a different page after refreshing?