Getting Started

Creating a Single-page Application with Vue + Vue Router feels natural: with Vue.js, we are already composing our application with components. When adding Vue Router to the mix, all we need to do is map our components to the routes and let Vue Router know where to render them. Here’s a basic example:

HTML

  1. <script src="https://unpkg.com/vue@3"></script>
  2. <script src="https://unpkg.com/vue-router@4"></script>
  3. <div id="app">
  4. <h1>Hello App!</h1>
  5. <p>
  6. <!-- use the router-link component for navigation. -->
  7. <!-- specify the link by passing the `to` prop. -->
  8. <!-- `<router-link>` will render an `<a>` tag with the correct `href` attribute -->
  9. <router-link to="/">Go to Home</router-link>
  10. <router-link to="/about">Go to About</router-link>
  11. </p>
  12. <!-- route outlet -->
  13. <!-- component matched by the route will render here -->
  14. <router-view></router-view>
  15. </div>

router-link

Note how instead of using regular a tags, we use a custom component router-link to create links. This allows Vue Router to change the URL without reloading the page, handle URL generation as well as its encoding. We will see later how to benefit from these features.

router-view

router-view will display the component that corresponds to the url. You can put it anywhere to adapt it to your layout.

JavaScript

  1. // 1. Define route components.
  2. // These can be imported from other files
  3. const Home = { template: '<div>Home</div>' }
  4. const About = { template: '<div>About</div>' }
  5. // 2. Define some routes
  6. // Each route should map to a component.
  7. // We'll talk about nested routes later.
  8. const routes = [
  9. { path: '/', component: Home },
  10. { path: '/about', component: About },
  11. ]
  12. // 3. Create the router instance and pass the `routes` option
  13. // You can pass in additional options here, but let's
  14. // keep it simple for now.
  15. const router = VueRouter.createRouter({
  16. // 4. Provide the history implementation to use. We are using the hash history for simplicity here.
  17. history: VueRouter.createWebHashHistory(),
  18. routes, // short for `routes: routes`
  19. })
  20. // 5. Create and mount the root instance.
  21. const app = Vue.createApp({})
  22. // Make sure to _use_ the router instance to make the
  23. // whole app router-aware.
  24. app.use(router)
  25. app.mount('#app')
  26. // Now the app has started!

By calling app.use(router), we get access to it as this.$router as well as the current route as this.$route inside of any component:

  1. // Home.vue
  2. export default {
  3. computed: {
  4. username() {
  5. // We will see what `params` is shortly
  6. return this.$route.params.username
  7. },
  8. },
  9. methods: {
  10. goToDashboard() {
  11. if (isAuthenticated) {
  12. this.$router.push('/dashboard')
  13. } else {
  14. this.$router.push('/login')
  15. }
  16. },
  17. },
  18. }

To access the router or the route inside the setup function, call the useRouter or useRoute functions. We will learn more about this in the Composition API

Throughout the docs, we will often use the router instance. Keep in mind that this.$router is exactly the same as directly using the router instance created through createRouter. The reason we use this.$router is because we don’t want to import the router in every single component that needs to manipulate routing.