Vue Learning - VueRouter Routing Basics

Vue Learning - VueRouter Routing Basics

1. VueRouter

1. Description

It feels natural to create single-page applications with Vue.js + Vue Router: with Vue.js , we can already compose applications by combining components. When you want to add Vue Router to it, all we need to do is map components to routes and then tell Vue Router 在where to render them. Routes are essentially hyperlinks.

2. Rendering of selected route:

(1) router-link-exact-active class

When routing to a certain place, the class name is added to the corresponding routing tag.
For example: When you click About, the route jumps to the page corresponding to About.

(2) router-link-active class

In routing, the path setting of the child route (for example: http://localhost/home) includes the path setting of the parent route (for example: http://localhost/). Then when the child route is clicked, when router-link-active class is added to the child route, the parent route also has router-link-active class. That is, when you click the (http://localhost/home) route, both routes are selected.

3. Basic working principle

Introduce router-link in the html part of vue (similar to the principle of a), the to attribute is the vue component to be jumped, and router-view is responsible for displaying the content pointed to by the current route, so that a single page can also achieve the effect of page jump!

2. Actual Combat

1. Create a vue project with router

2. Open the src/router/index.js file in the project

You can see that the project has automatically generated two routes, one is the home page and the other is about interface. The paths are '/' and '/about' respectively.

Open main.js in the root directory, and you can see that routing is introduced in main.js, so routing can be used in all components.

3. Open the project in the browser

You can see two route navigations: Home and About .

4. Create a new route

Write a route navigation similar to Taobao, including four parts: home page, message, shopping cart and me.

Create four new vue files, corresponding to four routes.

Configure the routing index.js file

import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/message',
    name: 'Message',
    component: () => import(/* webpackChunkName: "about" */ '../views/Message.vue')
  },
  {
    path: '/goodcar',
    name: 'GoodCar',
    component: () => import(/* webpackChunkName: "about" */ '../views/GoodCar.vue')
  },
  {
    path: '/me',
    name: 'Me',
    component: () => import(/* webpackChunkName: "about" */ '../views/Me.vue')
  }
]

const router = new VueRouter({
  routes
})

export default router

Configure the navigation bar in App.vue

<template>
  <div id="app">
    <div id="nav">
      <router-link to="/">Home</router-link>
      <router-link to="/message">message</router-link>
      <router-link to="/goodcar">Shopping Cart</router-link>
      <router-link to="/me">My</router-link>
    </div>
    <router-view/>
  </div>
</template>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
}

#nav {
  padding: 30px;
  margin: 0 auto;
  width: 30%;
  display: flex;
  justify-content: space-around;
}

#nav a {
  font-weight: bold;
  color: #2c3e50;
  text-decoration: none;
}

#nav a.router-link-exact-active {
  color: #42b983;
}
</style>

result:

So we understand the basic concept of routing and its configuration and its functions.

This is the end of this article about Vue Learning - VueRouter Routing Basics. For more relevant VueRouter Routing Basics content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Vue-router Quick Start
  • Vue routing vue-router usage explanation
  • Detailed explanation of how to use Vue.js routing component vue-router
  • Vue learning notes advanced version of vue-router installation and usage
  • vue-router: How to use nested routes
  • 10-minute quick start VueRouter 4.x tutorial

<<:  Implementation of comparison, sorting and other operations on varchar type dates in MySQL

>>:  Introduction to Apache deployment of https in cryptography

Recommend

Docker implements container port binding local port

Today, I encountered a small problem that after s...

Vue implements small notepad function

This article example shares the specific code of ...

Implementing a shopping cart with native JavaScript

This article shares the specific code of JavaScri...

Cross-origin image resource permissions (CORS enabled image)

The HTML specification document introduces the cr...

Summary of some common uses of refs in React

Table of contents What are Refs 1. String type Re...

How to install and connect Navicat in MySQL 8.0.20 and what to pay attention to

Things to note 1. First, you need to create a my....

Thumbnail hover effect implemented with CSS3

Achieve resultsImplementation Code html <heade...

How to enable the root account in Ubuntu 20.04

After Ubuntu 20.04 is installed, there is no root...

CSS Paint API: A CSS-like Drawing Board

1. Use Canvas images as CSS background images The...

HTML image img tag_Powernode Java Academy

summary Project description format <img src=&q...

Vue+echarts realizes stacked bar chart

This article shares the specific code of Vue+echa...

Native js to achieve seamless carousel effect

Native js realizes the carousel effect (seamless ...

About the usage and precautions of promise in javascript (recommended)

1. Promise description Promise is a standard buil...

The difference between JS pre-parsing and variable promotion in web interview

Table of contents What is pre-analysis? The diffe...