Proper component names

This commit is contained in:
Joan Sanchez 2022-03-28 09:06:36 +02:00
parent 6141f9a136
commit dc1f75b5e9
9 changed files with 81 additions and 34 deletions

View File

@ -1,6 +1,5 @@
<script lang="ts" setup> <script setup>
import { ref, Ref } from 'vue'; import { ref } from 'vue';
import { RouteRecordRaw } from 'vue-router';
import { useI18n } from 'vue-i18n'; import { useI18n } from 'vue-i18n';
import { useRole } from 'src/composables/useRole'; import { useRole } from 'src/composables/useRole';
import routes from 'src/router/routes'; import routes from 'src/router/routes';
@ -9,30 +8,17 @@ const { t } = useI18n();
const { hasAny } = useRole(); const { hasAny } = useRole();
const mainRoute = routes.find(route => route.path === '/'); const mainRoute = routes.find(route => route.path === '/');
const modules: RouteRecordRaw[] = mainRoute && mainRoute.children || [] const modules = mainRoute && mainRoute.children || []
interface MenuItem { const items = ref([]);
path: string;
title?: string;
icon?: string;
roles: string[];
}
interface ItemMeta {
title?: string;
icon?: string;
roles?: string[];
}
const items: Ref<MenuItem[]> = ref([]);
for (const module of modules) { for (const module of modules) {
const item: MenuItem = { const item = {
path: module.path, path: module.path,
roles: [] roles: []
}; };
if (module.meta) { if (module.meta) {
const meta: ItemMeta = module.meta; const meta = module.meta;
item.title = meta.title; item.title = meta.title;
item.icon = meta.icon; item.icon = meta.icon;
item.roles = meta.roles || []; item.roles = meta.roles || [];

View File

@ -1,10 +1,9 @@
import { ComputedRef } from 'vue';
import { useState } from './useState'; import { useState } from './useState';
export function useRole() { export function useRole() {
function hasAny(roles: string[]): boolean { function hasAny(roles) {
const { getRoles } = useState(); const { getRoles } = useState();
const roleStore: ComputedRef<string[]> = getRoles(); const roleStore = getRoles();
for (const role of roles) { for (const role of roles) {
if (roleStore.value.indexOf(role) !== -1) return true; if (roleStore.value.indexOf(role) !== -1) return true;

View File

@ -1,6 +1,11 @@
import { route } from 'quasar/wrappers'; import { route } from 'quasar/wrappers';
import { createRouter, createMemoryHistory, createWebHistory, createWebHashHistory } from 'vue-router'; import { createRouter, createMemoryHistory, createWebHistory, createWebHashHistory } from 'vue-router';
import routes from './routes'; import routes from './routes';
import { i18n } from 'src/boot/i18n';
import { useSession } from 'src/composables/useSession';
import { useRole } from 'src/composables/useRole';
const session = useSession();
const role = useRole();
/* /*
* If not building with SSR mode, you can * If not building with SSR mode, you can
@ -28,5 +33,62 @@ export default route(function (/* { store, ssrContext } */) {
history: createHistory(process.env.MODE === 'ssr' ? void 0 : process.env.VUE_ROUTER_BASE), history: createHistory(process.env.MODE === 'ssr' ? void 0 : process.env.VUE_ROUTER_BASE),
}); });
Router.beforeEach((to, from, next) => {
const { isLoggedIn } = session;
if (!isLoggedIn && to.name !== 'Login') {
next({ path: '/login', query: { redirect: to.fullPath } });
} else {
const pathRoutes = to.matched;
const droles = pathRoutes.every(route => {
const meta = route.meta;
if (meta.roles)
return role.hasAny(meta.roles)
return true;
});
if (droles) {
next();
} else {
next({ path: '/' });
}
}
});
Router.afterEach((to) => {
const { t } = i18n.global;
let title = '';
if (to.matched && to.matched.length > 2) {
const parent = to.matched[1];
const parentMeta = parent.meta;
if (parentMeta && parentMeta.title) {
title += t(`pages.${parentMeta.title}`);
}
}
const childMeta = to.meta;
if (childMeta && childMeta.title) {
if (title != '') title += ': ';
const childTitle = t(`pages.${childMeta.title}`);
if (to.params && to.params.id) {
const idParam = to.params.id;
if (idParam instanceof Array) {
title += `${idParam[0]} - ${childTitle}`
} else {
title += `${idParam} - ${childTitle}`;
}
} else {
title += t(`pages.${childMeta.title}`);
}
}
document.title = title;
});
return Router; return Router;
}); });

View File

@ -5,7 +5,7 @@ export default {
title: 'customers', title: 'customers',
icon: 'vn:client' icon: 'vn:client'
}, },
component: () => import('src/pages/Customer/Customer.vue'), component: () => import('src/pages/Customer/CustomerLayout.vue'),
redirect: { path: '/customer/list' }, redirect: { path: '/customer/list' },
children: [ children: [
{ {
@ -13,11 +13,11 @@ export default {
meta: { meta: {
title: 'list' title: 'list'
}, },
component: () => import('src/pages/Customer/List.vue'), component: () => import('src/pages/Customer/CustomerList.vue'),
}, },
{ {
path: '/customer/:id', path: '/customer/:id',
component: () => import('src/pages/Customer/Card/Card.vue'), component: () => import('src/pages/Customer/Card/CustomerCard.vue'),
redirect: { name: 'BasicData' }, redirect: { name: 'BasicData' },
children: [ children: [
{ {
@ -26,7 +26,7 @@ export default {
meta: { meta: {
title: 'basicData' title: 'basicData'
}, },
component: () => import('src/pages/Customer/Card/BasicData.vue'), component: () => import('src/pages/Customer/Card/CustomerBasicData.vue'),
} }
] ]
}, },

View File

@ -5,14 +5,14 @@ export default {
title: 'tickets', title: 'tickets',
icon: 'vn:ticket', icon: 'vn:ticket',
}, },
component: () => import('src/pages/Ticket/Ticket.vue'), component: () => import('src/pages/Ticket/TicketLayout.vue'),
redirect: { path: '/ticket/list'}, redirect: { path: '/ticket/list' },
children: [ children: [
{ {
path: 'list', path: 'list',
name: 'List', name: 'TicketList',
meta: { title: 'list' }, meta: { title: 'list' },
component: () => import('src/pages/Ticket/List.vue'), component: () => import('src/pages/Ticket/TicketList.vue'),
} }
], ],
}; };