Skip to content

Instantly share code, notes, and snippets.

<template>
<div>
<h1>This is an users page</h1>
<router-view />
</div>
</template>
<script>
export default {
name: 'Users'
export default pages
// Note: remove nested routes from pages
.filter(path => !path.some(childrenFilter))
.map(async path => {
const { default: component } = await import(`../views/${path.join('/')}`)
const { name } = component
const route = `/${generateRoute([...path])}`
let children = []
if (childrenByPath[route]) {
const promises = childrenByPath[route].map(async ({ path, route }) => {
const childrenFilter = p => ~p.indexOf('^')
const childrenByPath = pages
// Note: filter pages by children routes
.filter(path => path.some(childrenFilter))
.map(path => {
// Note: copy path and remove special char ^
const copy = [...path]
copy[copy.length - 1] = copy[copy.length - 1].slice(1)
// Note: generate key to identify parent
const generateRoute = path => {
// Note: remove first element if route starts with index
if (path[0].toLowerCase().startsWith('index') && path.length > 1) {
path.shift()
}
// Note: handle root routes
if (path.length === 1) {
const shortcut = path[0].toLowerCase()
return shortcut.startsWith('index')
? ''
<template>
<div>
<h1>This is a page to edit the post with id {{ $route.params.id }}</h1>
</div>
</template>
<script>
export default {
name: "PostEdit"
}
<template>
<div>
<h1>This is a page of the post with id {{ $route.params.id }}</h1>
</div>
</template>
<script>
export default {
name: "PostDetails"
}
<template>
<div>
<h1>This is a post creation page</h1>
</div>
</template>
<script>
export default {
name: "PostCreate"
}
<template>
<div>
<h1>This is a list of posts page</h1>
</div>
</template>
<script>
export default {
name: "Posts"
}
const generateRoute = path => {
// Note: remove first element if route starts with index
if (path[0].toLowerCase().startsWith('index') && path.length > 1) {
path.shift()
}
// Note: handle root routes
if (path.length === 1) {
const shortcut = path[0].toLowerCase()
return shortcut.startsWith('index')
? ''
import { createApp } from 'vue'
import App from './App.vue'
const init = async() => {
const module = await import('./router');
const router = await module.default;
createApp(App).use(router).mount('#app')
}
init()