search
HomeWeb Front-endVue.jsLet's talk in depth about Router routing in Vue

This article will take you to understand Vue and introduce in detail Vue Router routing in the essential knowledge for getting started. I hope it will be helpful to everyone!

Let's talk in depth about Router routing in Vue

1. The concept and principle of front-end routing

(1) What is routing

Routing (English: router) is the corresponding relationship.

(2) SPA and front-end routing

SPA refers to a web website that has only one HTML page, display and switching of all components are all completed in this only page. At this time, Switching between different components needs to be achieved through front-end routing. (Learning video sharing: vue video tutorial)

?Conclusion?: In the SPA project, switching between different functions depends on the front-end Routing to complete!

(3) What is front-end routing

Easy-to-understand concept:

Hash address and component Correspondence.

(4) How front-end routing works

  • User

    clicked the routing link# on the page ##;

  • caused the
  • Hash value

    in the URL address bar to change;

  • The front-end routing monitors the change of the Hash address;

  • The front-end routing renders the component
  • corresponding to the current

    Hash address in the browser ;

?Conclusion?: Front-end routing refers to the Lets talk in depth about Router routing in Vue
Hash address and Correspondence between components !

(5) Implement simple front-end routing

1️⃣ Step 1: Pass the

tag, combined with comName Dynamic rendering components. The sample code is as follows:

2️⃣ In the App.vue component, add the corresponding Lets talk in depth about Router routing in Vuehash value
for the <a> link</a>. The sample code is as follows:

3️⃣ In the Lets talk in depth about Router routing in Vuecreated
life cycle function, monitor the change of the hash address in the browser address bar, and dynamically switch the The name of the displayed component. The sample code is as follows:

Lets talk in depth about Router routing in Vue


2. Basic use of vue-router

(1) What is vue-router

vue-router

is the official routing solution given by vue.js. It can only be used in conjunction with vue projects, and can easily manage the switching of components in SPA projects. ?The official document address of vue-router?:

https://router.vuejs.org/zh/

(2) vue-router installation And configuration steps

    Installation
  • vue-router

    Package

  • Create routing module
  • Import and mount the routing module
  • Declare routing links and placeholders
2.1 In the project Install vue-router

The command to install

vue-router

is as follows:<pre class="brush:php;toolbar:false">npm install vue-router</pre>

2.2 Create routing module

In the

src

source code directory, create a new router/index.js routing module and initialize the following code:

Lets talk in depth about Router routing in Vue

2.3 Import and mount the routing module

In the

src/main.js

entry file, import and mount the routing module. The sample code is as follows:

In the src/App.vue component, use the <router-link></router-link> provided by vue-router and <router-view></router-view> Declare routing links and placeholders:

Lets talk in depth about Router routing in Vue

(3) Declare routing matching rules

In the src/router/index.js routing module, declare the matching rules of the route through the routes array. The sample code is as follows:

Lets talk in depth about Router routing in Vue


3. Common usage of vue-router

(1) Route re-routing Direction

Route redirection refers to: when the user accesses address A , forces the user to jump to address C , thereby displaying a specific component page. By specifying a new routing address through the redirect attribute of the routing rule, you can easily set the redirection of the routing:

Lets talk in depth about Router routing in Vue

(2 ) Nested routing

realizes the nested display of components through routing, which is called nested routing.

Lets talk in depth about Router routing in Vue

In the About.vue component, declare tab1 and tab2Subroute link and Subroute placeholder. The sample code is as follows:

Lets talk in depth about Router routing in Vue

2.2 Declare sub-routing rules through the children attribute

In the src/router/index.js routing module , import the required components, and use the children attribute to declare sub-routing rules:

Lets talk in depth about Router routing in Vue

(3) Dynamic routing matching

3.1 The concept of dynamic routing

1️⃣ Dynamic routing refers to: defining the variable part of the Hash address as a parameter item, thereby improving the reusability of routing rules .
2️⃣ Use English colon (:) in vue-router to define routing parameters. The sample code is as follows:

Lets talk in depth about Router routing in Vue

##3.2 $route.params parameter object

is rendered in

dynamic routing In the component, you can use the this.$route.params object to access the dynamically matched parameter value.

Lets talk in depth about Router routing in Vue

3.3 Use props to receive routing parameters

In order to simplify the form of obtaining routing parameters, vue-router Allow props parameter passing to be enabled in routing rules. The sample code is as follows:

Lets talk in depth about Router routing in Vue

(4) Declarative Navigation & Programmatic Navigation

In the browser,

click The way links implement navigation is called declarative navigation. For example:

In the browser, the

call API method to implement navigation is called programmed navigation. For example:

    The method of calling
  • location.href in an ordinary web page to jump to a new page is programmatic navigation;

( 5) Programmatic navigation API in vue-router

vue-router provides many programmatic navigation APIs, among which the most commonly used navigation APIs are:

  1. this.$router.push('hash address'): Jump to the specified hash address and add a history record;
  2. this.$ router.replace('hash address'): Jump to the specified hash address and replace the current history record;
  3. this.$router.go(value n): Implement navigation history forward and backward;

5.1 $router.push

Call this.$router.push() Method, you can jump to the specified hash address to display the corresponding component page. The sample code is as follows:

Lets talk in depth about Router routing in Vue

##5.2 $router.replace

Call

this.$router.replace() Method, you can jump to the specified hash address to display the corresponding component page;

?The difference between push and replace?:

    push will
  • add a history Record;
  • replace will not add the history record, but
  • replace the current history record;
##5.3 $router.go

Call the

this.$router.go()

method to move forward and backward in the browsing history. The sample code is as follows:

Lets talk in depth about Router routing in Vue##5.4 Simplified usage of $router.go

In actual development, generally only forward and backward One layer of pages. Therefore, vue-router provides the following two convenient methods:

$router.back()
    : In the history,
  1. Back to the previous page $router.forward()
  2. : In the history,
  3. Forward to the next page
  4. (6) Navigation Guard

Navigation guard can control access permissions of routes.

The schematic diagram is as follows:

Lets talk in depth about Router routing in Vue6.1 Global front guard

Every time a routing navigation jump occurs , will trigger the global front guard.

Therefore, in the global front guard, programmers can control the access permissions of each route:

Lets talk in depth about Router routing in Vue6.2 3 forms of guard methods The callback function of parameter

global front guard

receives 3 formal parameters in the format:

Lets talk in depth about Router routing in Vue6.3 Three ways to call the next function

Refer to the diagram to analyze the final results of the three ways to call the next function:

Lets talk in depth about Router routing in VueThe current user

has the access rights to the
    backend homepage, and is directly allowed: next()
  • The current user does not have the access rights to the
  • backend homepage,
  • forces it to jump Go to the login page: next('/login')The current userdoes not have access rights to the
  • backend homepage,
  • is not allowed to jump to the backend homepage: next(false)
  • 6.4 Control access to the background homepage

(Learning video sharing: Lets talk in depth about Router routing in Vueweb Front-end development

,

Basic programming video)

The above is the detailed content of Let's talk in depth about Router routing in Vue. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:csdn. If there is any infringement, please contact admin@php.cn delete
The Frontend Landscape: How Netflix Approached its ChoicesThe Frontend Landscape: How Netflix Approached its ChoicesApr 15, 2025 am 12:13 AM

Netflix's choice in front-end technology mainly focuses on three aspects: performance optimization, scalability and user experience. 1. Performance optimization: Netflix chose React as the main framework and developed tools such as SpeedCurve and Boomerang to monitor and optimize the user experience. 2. Scalability: They adopt a micro front-end architecture, splitting applications into independent modules, improving development efficiency and system scalability. 3. User experience: Netflix uses the Material-UI component library to continuously optimize the interface through A/B testing and user feedback to ensure consistency and aesthetics.

React vs. Vue: Which Framework Does Netflix Use?React vs. Vue: Which Framework Does Netflix Use?Apr 14, 2025 am 12:19 AM

Netflixusesacustomframeworkcalled"Gibbon"builtonReact,notReactorVuedirectly.1)TeamExperience:Choosebasedonfamiliarity.2)ProjectComplexity:Vueforsimplerprojects,Reactforcomplexones.3)CustomizationNeeds:Reactoffersmoreflexibility.4)Ecosystema

The Choice of Frameworks: What Drives Netflix's Decisions?The Choice of Frameworks: What Drives Netflix's Decisions?Apr 13, 2025 am 12:05 AM

Netflix mainly considers performance, scalability, development efficiency, ecosystem, technical debt and maintenance costs in framework selection. 1. Performance and scalability: Java and SpringBoot are selected to efficiently process massive data and high concurrent requests. 2. Development efficiency and ecosystem: Use React to improve front-end development efficiency and utilize its rich ecosystem. 3. Technical debt and maintenance costs: Choose Node.js to build microservices to reduce maintenance costs and technical debt.

React, Vue, and the Future of Netflix's FrontendReact, Vue, and the Future of Netflix's FrontendApr 12, 2025 am 12:12 AM

Netflix mainly uses React as the front-end framework, supplemented by Vue for specific functions. 1) React's componentization and virtual DOM improve the performance and development efficiency of Netflix applications. 2) Vue is used in Netflix's internal tools and small projects, and its flexibility and ease of use are key.

Vue.js in the Frontend: Real-World Applications and ExamplesVue.js in the Frontend: Real-World Applications and ExamplesApr 11, 2025 am 12:12 AM

Vue.js is a progressive JavaScript framework suitable for building complex user interfaces. 1) Its core concepts include responsive data, componentization and virtual DOM. 2) In practical applications, it can be demonstrated by building Todo applications and integrating VueRouter. 3) When debugging, it is recommended to use VueDevtools and console.log. 4) Performance optimization can be achieved through v-if/v-show, list rendering optimization, asynchronous loading of components, etc.

Vue.js and React: Understanding the Key DifferencesVue.js and React: Understanding the Key DifferencesApr 10, 2025 am 09:26 AM

Vue.js is suitable for small to medium-sized projects, while React is more suitable for large and complex applications. 1. Vue.js' responsive system automatically updates the DOM through dependency tracking, making it easy to manage data changes. 2.React adopts a one-way data flow, and data flows from the parent component to the child component, providing a clear data flow and an easy-to-debug structure.

Vue.js vs. React: Project-Specific ConsiderationsVue.js vs. React: Project-Specific ConsiderationsApr 09, 2025 am 12:01 AM

Vue.js is suitable for small and medium-sized projects and fast iterations, while React is suitable for large and complex applications. 1) Vue.js is easy to use and is suitable for situations where the team is insufficient or the project scale is small. 2) React has a richer ecosystem and is suitable for projects with high performance and complex functional needs.

How to jump a tag to vueHow to jump a tag to vueApr 08, 2025 am 09:24 AM

The methods to implement the jump of a tag in Vue include: using the a tag in the HTML template to specify the href attribute. Use the router-link component of Vue routing. Use this.$router.push() method in JavaScript. Parameters can be passed through the query parameter and routes are configured in the router options for dynamic jumps.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software