Example of Vue routing listening to dynamically load the same page

Example of Vue routing listening to dynamically load the same page

Scenario Analysis

In the system, one module has three sub-modules. In the business data, the three sub-modules can be directly distinguished according to the type. Usually, we write in the same module and then select the business type. However, the owner requires that this be split into three menus. Users can choose the module they need to use according to their needs.

These three menus use the same data table. So we must only write one list, add, and edit page. Then determine which category it belongs to based on the route of entering the page. And jump to the add, edit, and call the corresponding list interface page of the specified category

Development

Since the three modules use the same page, three routes need to be configured and the pages need to be distinguished. The problem we are facing now is that although the three menus have different routes, they are the same page. When switching menus, the hook function of Vue will not be triggered. Then the data queried by the three list pages is the same, which means that the query method will not be triggered, resulting in the inability to switch conditions.

Then I searched the Internet for watch monitoring time. I found that I can trigger the list data loading method when the route changes by monitoring. The specific code is as follows

The routes of the list pages are added with 1, 2, and 3 after the list to distinguish which page it is.

watch:
    '$route.path': function (newVal, oldVal) {
        // Parameters of new and old routing path type are three types of menus defined globally this.type = newVal.substr(newVal.lastIndexOf("/") + 1);
        this.search();
    }
},


Then when you switch routes, you can re-pull the list data. At the same time, the create method also needs to call search. Because the route monitoring only works on this page. It will not be triggered when other pages switch routes to this menu.

created () {
    let path = this.$route.path;
    this.type = path.substr(path.lastIndexOf("/") + 1);
    this.search();
},

Summarize

In actual development, you can choose the monitoring attributes according to your needs

watch:{  
    //Monitor route changes $route( to , from ){   
       console.log( to , from )
        // to , from represent where to jump to respectively, both are objects // to.path (represents the address of the route to jump to);
     }
}

The above is the details of the example of Vue routing listening to achieve dynamic loading of the same page. For more information about Vue routing listening to achieve dynamic loading of the same page, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Reasons and solutions for multiple executions of the watch method when Vue monitors route changes
  • Vue uses routing hook interceptors beforeEach and afterEach to listen to routing
  • Vue listens to the browser's native return button to perform route jump operations
  • Vue listens to route changes and refreshes the page in the App.vue file
  • Vue routing cache routing nested routing guard listens to physical return operations
  • Summary of several ways for Vue to monitor route changes

<<:  What to do if the container started by docker run hangs and loses data

>>:  Docker: Modifying the DOCKER_OPTS parameter in /etc/default/docker does not take effect

Recommend

Detailed explanation of Vue's methods and properties

Vue methods and properties 1. Methods Usage 1 met...

HTML discount price calculation implementation principle and script code

Copy code The code is as follows: <!DOCTYPE HT...

Navicat remote connection to MySQL implementation steps analysis

Preface I believe that everyone has been developi...

Are you still Select *?

There are many reasons why an application is as s...

Example of MySQL auto-increment ID exhaustion

Display Definition ID When the auto-increment ID ...

Why the explain command may modify MySQL data

If someone asked you whether running EXPLAIN on a...

Detailed explanation of docker command to backup linux system

tar backup system sudo tar cvpzf backup.tgz --exc...

Code to display the contents of a txt book on a web page

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML ...

Detailed explanation of Bootstrap grid vertical and horizontal alignment

Table of contents 1. Bootstrap Grid Layout 2. Ver...

Method example of safely getting deep objects of Object in Js

Table of contents Preface text parameter example ...

50 lines of code to implement Webpack component usage statistics

background Recently, a leader wanted us to build ...

Detailed explanation of JavaScript animation function encapsulation

Table of contents 1. Principle of animation funct...

Detailed explanation of the use of Arguments object in JavaScript

Table of contents Preface Basic Concepts of Argum...