About.vue:

Now you can run your application with the command:

npm run serve

Open your browser and go to http://localhost:8080. Try clicking on the Home and About links to see how routing works!

Conclusion

In this post, we’ve covered the basics of routing in Vue.js using Vue Router. With this routing capability, you can build more interactive and user-friendly applications. In the next post, we can delve into more advanced features of Vue Router, such as route parameters and nested routes.

I hope you found this post helpful! Feel free to leave a comment if you have any questions.

","image":"http://www.luping.net/uploads/20241015/1728950771670db1f354034.jpg","datePublished":"2024-11-01T00:30:35+08:00","dateModified":"2024-11-01T00:30:35+08:00","author":{"@type":"Person","name":"luping.net","url":"https://www.luping.net/articlelist/0_1.html"}}
"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Vue.js for Beginners A Complete Guide to Routing with Vue Router

Vue.js for Beginners A Complete Guide to Routing with Vue Router

Published on 2024-11-01
Browse:217

Vue.js for Beginners A Complete Guide to Routing with Vue Router

Understanding Routing in Vue.js

Routing is an essential feature in web application development. In Vue.js, we can use Vue Router to easily manage navigation between pages. In this post, we’ll explore how to set up routing in a Vue.js application.

What is Vue Router?

Vue Router is the official library for managing routing in Vue.js applications. With Vue Router, we can create single-page applications (SPAs) that allow smooth navigation without reloading the page.

Installing Vue Router

First, we need to install Vue Router. If you are using Vue CLI, you can select to install it while creating a new project. If not, you can install it with the following command:

npm install vue-router

Setting Up Vue Router

After installation, we need to configure Vue Router. Here are the basic steps to set it up:

  • Create a Router File Create a new file named router.js inside the src folder: import Vue from 'vue'; import Router from 'vue-router'; import Home from './views/Home.vue'; import About from './views/About.vue';
Vue.use(Router);

export default new Router({
  mode: 'history', // Using history mode for cleaner URLs
  routes: [
    {
      path: '/',
      name: 'home',
      component: Home,
    },
    {
      path: '/about',
      name: 'about',
      component: About,
    },
  ],
});

  • Integrate Router into the Application Next, we need to integrate the router into our Vue instance. Edit the main.js file as follows:
import Vue from 'vue';
import App from './App.vue';
import router from './router';

new Vue({
  el: '#app',
  router, // Add the router to the Vue instance
  render: h => h(App),
});
  • Using Router Links To navigate between pages, we use the component. Here’s an example of how to use it in the App.vue file:

  • Creating New Pages Now, let’s create two components that we will use: Home.vue and About.vue.

Home.vue:




About.vue:




Now you can run your application with the command:

npm run serve

Open your browser and go to http://localhost:8080. Try clicking on the Home and About links to see how routing works!

Conclusion

In this post, we’ve covered the basics of routing in Vue.js using Vue Router. With this routing capability, you can build more interactive and user-friendly applications. In the next post, we can delve into more advanced features of Vue Router, such as route parameters and nested routes.

I hope you found this post helpful! Feel free to leave a comment if you have any questions.

Release Statement This article is reproduced at: https://dev.to/kyydev/vuejs-for-beginners-2024-a-complete-guide-to-routing-with-vue-router-24bh?1 If there is any infringement, please contact study_golang@163 .comdelete
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3