Last active
April 28, 2018 01:17
-
-
Save Tattomoosa/f28f8c2d725682ddf519c5b9b3025b4e to your computer and use it in GitHub Desktop.
Vue Router component that auto populates with links to every route
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- inspired by https://stackoverflow.com/questions/36120996/get-all-routes-in-a-vue-router/36128368 --> | |
<template> | |
<div> | |
<span v-for="route in routes" :key="route.name"> | |
<router-link :to="route.path">{{ route.name }}</router-link> | |
</span> | |
</div> | |
</template> | |
<script> | |
export default { | |
name: 'Navigation-Menu', | |
created () { | |
// This dynamically grabs every route in router | |
this.$router.options.routes.forEach(route => { | |
this.routes.push({ | |
name: route.name, | |
path: route.path | |
}) | |
}) | |
}, | |
data () { | |
return { | |
routes: [] | |
} | |
} | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment