In this section, we will cover how to set up Vue Router in your Vue.js application. Vue Router is the official router for Vue.js, enabling you to create single-page applications with navigation and dynamic routing.
Prerequisites
Before we start, ensure you have:
- Node.js and npm installed on your machine.
- A basic understanding of Vue.js and how to create a Vue application.
Steps to Set Up Vue Router
- Install Vue Router
First, you need to install Vue Router. You can do this using npm or yarn.
- Create a Vue Application
If you don't already have a Vue application, you can create one using Vue CLI.
- Set Up the Router
Create a router
directory in the src
folder and add an index.js
file inside it.
- Configure Routes
In the src/router/index.js
file, configure your routes. Here’s an example with two simple routes:
import { createRouter, createWebHistory } from 'vue-router'; import Home from '../views/Home.vue'; import About from '../views/About.vue'; const routes = [ { path: '/', name: 'Home', component: Home }, { path: '/about', name: 'About', component: About } ]; const router = createRouter({ history: createWebHistory(process.env.BASE_URL), routes }); export default router;
- Create View Components
Create the Home.vue
and About.vue
components in the src/views
directory.
Add some basic content to these components:
Home.vue
<template> <div> <h1>Home Page</h1> <p>Welcome to the Home Page!</p> </div> </template> <script> export default { name: 'Home' }; </script>
About.vue
<template> <div> <h1>About Page</h1> <p>This is the About Page.</p> </div> </template> <script> export default { name: 'About' }; </script>
- Integrate Router with Vue Application
In your src/main.js
file, import the router and add it to your Vue application.
import { createApp } from 'vue'; import App from './App.vue'; import router from './router'; createApp(App) .use(router) .mount('#app');
- Update App Component
Update your App.vue
to include the <router-view>
component, which will render the matched component for the current route.
<template> <div id="app"> <nav> <router-link to="/">Home</router-link> <router-link to="/about">About</router-link> </nav> <router-view></router-view> </div> </template> <script> export default { name: 'App' }; </script> <style> nav { margin-bottom: 20px; } nav a { margin-right: 10px; } </style>
- Run Your Application
Now, you can run your application to see Vue Router in action.
Open your browser and navigate to http://localhost:8080
. You should see the Home page. Click on the "About" link to navigate to the About page.
Summary
In this section, you learned how to set up Vue Router in a Vue.js application. You installed Vue Router, configured routes, created view components, and integrated the router with your Vue application. This setup allows you to create a single-page application with multiple views and navigation.
Next, we will explore dynamic routing, where you will learn how to create routes with dynamic segments and handle route parameters.
Vue.js Course
Module 1: Introduction to Vue.js
- What is Vue.js?
- Setting Up the Development Environment
- Creating Your First Vue Application
- Understanding the Vue Instance
Module 2: Vue.js Basics
- Template Syntax
- Data Binding
- Computed Properties and Watchers
- Class and Style Bindings
- Conditional Rendering
- List Rendering
Module 3: Vue.js Components
- Introduction to Components
- Props and Custom Events
- Slots
- Dynamic and Async Components
- Component Communication
Module 4: Vue Router
Module 5: State Management with Vuex
- Introduction to Vuex
- State, Getters, Mutations, and Actions
- Modules in Vuex
- Using Vuex in Components
- Advanced Vuex Patterns
Module 6: Vue.js Directives
Module 7: Vue.js Plugins
Module 8: Testing in Vue.js
Module 9: Advanced Vue.js Concepts
- Render Functions and JSX
- Server-Side Rendering (SSR) with Nuxt.js
- Vue 3 Composition API
- Performance Optimization