9951 explained code solutions for 126 technologies


vue.jsHow do I set up routing in a Vue.js application?


To set up routing in a Vue.js application, you need to use the Vue Router.

First, you need to install the Vue Router library:

npm install vue-router

Then, you need to import the library into your main.js file:

import VueRouter from 'vue-router'
Vue.use(VueRouter)

Next, you need to define the routes in your application. For example, you can define a route to the Home component:

const routes = [
  { path: '/', component: Home }
]

Then, you need to create a router instance and pass the routes to it:

const router = new VueRouter({
  routes
})

Finally, you need to add the router instance to your Vue instance:

new Vue({
  router
}).$mount('#app')

Now, you are ready to use the Vue Router in your application.

Edit this code on GitHub