Add custom body classes to the router file

This commit is contained in:
Daniel_I_Am 2021-09-01 23:38:59 +02:00
parent 624ad8ad86
commit 8d450f42b2
3 changed files with 30 additions and 3 deletions

View File

@ -22,4 +22,30 @@ const router = VueRouter.createRouter({
routes,
});
const classes = {
'index': ['page-home'],
};
router.afterEach((to, from, failure) => {
if (!failure) {
if (!!from && !!from.name) {
if (Object.keys(classes).includes(from.name.toString())) {
for (const className of classes[from.name.toString()]) {
if (document.body.classList.contains(className)) {
document.body.classList.remove(className);
}
}
}
}
if (!!to && !!to.name) {
if (Object.keys(classes).includes(to.name.toString())) {
for (const className of classes[to.name.toString()]) {
document.body.classList.add(className);
}
}
}
}
});
export default router

View File

@ -1,3 +1,5 @@
@extends('template.base')
@section('body-classes', 'page-home')
@push('js')
<script src="{{ asset('js/app.js') }}"></script>
@endpush

View File

@ -11,12 +11,11 @@
@stack('style')
</head>
<body class="@yield('body-classes')">
<body>
@section('body')
<div id="app"></div>
@show
<script src="{{ asset('js/app.js') }}"></script>
@stack('js')
</body>