The perfect solution for Vue routing fallback (vue-route-manager)

The perfect solution for Vue routing fallback (vue-route-manager)

Routing Manager

Record the vue-route name of each jump, and have built-in methods for handling fallbacks, making it easy to fall back to the specified page

background

In the projects developed by the author, various fancy jumps are often encountered. For example, from the selection operation on the guide page to the final submission and review, there will be countless pages in the middle, and even different operations in the middle will lead to different final rollback depths.

Assume that the starting point of the project is the selection page, and eventually it will reach the submission page and return to the original page (selection page)

Select Page --> B --> C --> Submit Page
Case 1: From selection to submission, we go through B and C. At this time, we need to call router.go(-3) to return to A.
----------------------------------------

Select Page --> B-1 ------> Submit Page
In case 2, only B-1 is experienced from selection to submission. At this time, go(-3) is no longer applicable. At this time, a query parameter (channel id) may be added to distinguish the second case.
----------------------------------------

Select Page --> B-2 --> C-2 --> C-2-1 --> Submit Page
In this case, not only is go(-3) not applicable, but the query parameter must have an additional type. If more channels need to be distinguished later, you can imagine...

At this time, you can use the RouteManager plug-in to handle this series of complex problems

getting Started

 npm i vue-route-manager -S
 import Vue from 'vue'
 ​
 // Import the route manager import VueRouteManager from 'vue-route-manager'
 ​
 // and mount it on Vue Vue.use(VueRouteManager, { /* ...ManagerOptions */ })
 ​
 // At this point in the page you can use this.$RouteManager to access the manager

ManagerOptions Parameters

Parameter name type must describe
router VueRouter Y VueRouter Object
debug Boolean N Whether to enable debugging

Example

Home Page

Route information { path: '/home', name: 'home', component: Home }

 <template>
  <button @click="jump">Next page</button>
 </template>
 <script>
 export default {
    methods: {
       jump(){
          //Record the name of the home page
          this.$RouteManager.setHome('home')
          this.$router.push({ name: 'page-1' })
      }
    }
 }
 </script>

Page-1 Page

Routing information { path: '/page_1', name: 'page-1', component: Page-1 }

 <template>
  <div class="page-1">
  page-1
  <button @click="$router.push({ name: 'page-2' })">Next page</button>
  <button @click="$router.replace({ name: 'page-1' })">Redirect</button>
  </div>
 </template>

Page-2

Route information { path: '/page_2', name: 'page-2', component: Page-2 }

 <template>
  <div class="page-2">
  page-2
  <button @click="$router.push({ name: 'page-3' })">Next page</button>
  <button @click="backToHome">Back to Home</button>
  </div>
 </template>
 <script>
 export default {
    methods: {
       backToHome(){
          // Call backHome to return to the home page of the first record this.$RouteManager.backHome()
      }
    }
 }
 </script>

Page-3 pages

Routing information { path: '/page_3', name: 'page-3', component: Page-3 }

 <template>
   <div class="page-3">
    page-3
     <button @click="$backToHome">Back to Home</button>
     <button @click="backToPage">Back to page-1</button>
   </div>
 </template>
 export default {
    methods: {
      backToPage(){
          // Call backByName to return to the specified page (must have gone through this page)
          this.$RouteManager.backByName('page-1')
      },
  backToHome(){
          // Call backHome to return to the home page of the first record this.$RouteManager.backHome()
      }
    }
 }
 </script>

Solving the problem

A --> B --> C --> D -- Return -> A // Case 1
|--> B-1 ------> D --Return-> A // Case 2
|--> B-2 --> C-2 -->C-2-1 --> D --Return-> A // Case 3

First, call this.$RouteManager.setHome('page-A') or this.$RouteManager.setHome() on page A

Then when page B needs to return, call this.$RouteManager.backHome()

Methods

setHome([name])

  • name
    • Type: String
    • name refers to the name in the route list { path: '/page_3', name: 'page-3', component: Page-3 }
    • Default: the name of the current route

Set the page route name that needs to be returned eventually

backHome()

Return to the home page and set the home page via setHome

backByName(name)

  • name
    • Type: String
    • name refers to the name in the route list { path: '/page_3', name: 'page-3', component: Page-3 }

Return to the page with the specified name

Summarize

This concludes this article about vue-route-manager, the perfect solution for Vue routing fallback. For more relevant Vue routing fallback 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:
  • In vue, it is forbidden to go back to the previous step, and the route does not store history

<<:  Solution to the problem of two slashes // appearing after the domain name when nginx is configured for domain name access

>>:  Best Practices Guide for MySQL Partitioned Tables

Recommend

Implementation of postcss-pxtorem mobile adaptation

Execute the command to install the plugin postcss...

Detailed steps for running springboot project in Linux Docker

Introduction: The configuration of Docker running...

Solve the problem of invalid utf8 settings in mysql5.6

After the green version of mysql5.6 is decompress...

Handwritten Vue2.0 data hijacking example

Table of contents 1: Build webpack 2. Data hijack...

Upgrading Windows Server 2008R2 File Server to Windows Server 2016

The user organization has two Windows Server 2008...

Detailed explanation of the failure of MySQL to use UNION to connect two queries

Overview UNION The connection data set keyword ca...

MySQL trigger syntax and application examples

This article uses examples to illustrate the synt...

Several ways to add timestamps in MySQL tables

Scenario: The data in a table needs to be synchro...

An article to help you learn more about JavaScript arrays

Table of contents 1. The role of array: 2. Defini...

A mobile adaptive web page effect solves the problem of small display page

For work needs, I need to make a mobile phone adap...

An example of using Lvs+Nginx cluster to build a high-concurrency architecture

Table of contents 1. Lvs Introduction 2. Lvs load...

Is it easy to encapsulate a pop-up component using Vue3?

Table of contents Summary put first: 🌲🌲 Preface: ...

Detailed tutorial on compiling and installing MySQL 8.0.20 from source code

In the previous article, we introduced: MySQL8.0....