Skip to content

Commit

Permalink
hashroutes!
Browse files Browse the repository at this point in the history
  • Loading branch information
sofiaedv committed Oct 14, 2024
1 parent 74c4bd8 commit b5713e9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 60 deletions.
41 changes: 0 additions & 41 deletions src/App.vue
Original file line number Diff line number Diff line change
@@ -1,45 +1,4 @@
<script>
import { RouterLink, RouterView } from 'vue-router'
import { routes } from './router';
</script>

<template>
<header>
<img alt="Vue logo" class="logo" src="@/assets/logo.svg" width="125" height="125" />

<div class="wrapper">
<HelloWorld msg="You did it!" />

<nav>
<RouterLink to="/">Home</RouterLink>
<RouterLink to="/about">About</RouterLink>
</nav>
</div>
</header>

<RouterView />
</template>

<style scoped>
header {
line-height: 1.5;
max-height: 100vh;
}
.logo {
display: block;
margin: 0 auto 2rem;
}
nav {
width: 100%;
font-size: 12px;
text-align: center;
margin-top: 2rem;
}
nav a.router-link-exact-active {
<script>
import { RouterLink, RouterView } from 'vue-router';
import { routes } from './router';
Expand Down
37 changes: 18 additions & 19 deletions src/router/index.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
import { createRouter, createWebHistory, createWebHashHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import { createRouter, createWebHashHistory } from 'vue-router';

// Automatically import all views from the "views" directory
const viewModules = import.meta.glob('../views/*.vue');

export const routes = Object.keys(viewModules).map((viewPath) => {
const name = viewPath.split('/').pop().replace('.vue', '');
const path = name === 'HomeView' ? '/' : `/${name.replace('View', '').toLowerCase()}`;

return {
path: path,
name: name.toLowerCase(),
component: viewModules[viewPath], // Lazy-loaded components
};
});

const router = createRouter({
history: createWebHashHistory(import.meta.env.BASE_URL),
routes: [
{
path: '/',
name: 'home',
component: HomeView
},
{
path: '/about',
name: 'about',
// 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('../views/AboutView.vue')
}
]
})
routes,
});

export default router
export default router;

0 comments on commit b5713e9

Please sign in to comment.