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

Troubleshooting ideas and solutions for high CPU usage in Linux systems

Preface As Linux operation and maintenance engine...

Detailed explanation of how Vue components transfer values ​​to each other

Table of contents Overview 1. Parent component pa...

Mysql case analysis of transaction isolation level

Table of contents 1. Theory SERIALIZABLE REPEATAB...

Parsing MySQL binlog

Table of contents 1. Introduction to binlog 2. Bi...

Nginx routing forwarding and reverse proxy location configuration implementation

Three ways to configure Nginx The first method di...

A brief analysis of MySQL cardinality statistics

1. What is the cardinality? Cardinality refers to...

React implements paging effect

This article shares the specific code for React t...

How to successfully retrieve VMware Esxi root password after forgetting it

Prepare a CentOS6 installation disk (any version)...

TimePicker in element disables part of the time (disabled to minutes)

The project requirements are: select date and tim...

Detailed explanation of process management in Linux system

Table of contents 1. The concept of process and t...

Tutorial on using Docker Compose to build Confluence

This article uses the "Attribution 4.0 Inter...

The difference between char, varchar and text field types in MySQL

In MySQL, fields of char, varchar, and text types...

A brief discussion on the principle of js QR code scanning login

Table of contents The essence of QR code login Un...