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

js implements custom drop-down box

This article example shares the specific code of ...

Detailed explanation of MySQL trigger trigger example

Table of contents What is a trigger Create a trig...

Vue implements a visual drag page editor

Table of contents Drag and drop implementation Dr...

MySQL sorting Chinese details and examples

Detailed explanation of MySQL sorting Chinese cha...

Detailed tutorial on installing CUDA9.0 on Ubuntu16.04

Preface: This article is based on the experience ...

Introduction to HTML basic controls_PowerNode Java Academy

The <input> tag The <input> tag is us...

Vue uses OSS to upload pictures or attachments

Use OSS to upload pictures or attachments in vue ...

MySQL service and database management

Table of contents 1. Start and stop service instr...

Two ways to declare private variables in JavaScript

Preface JavaScript is not like other languages ​​...

Implementation steps of mysql master-slave replication

Table of contents mysql master-slave replication ...

Detailed explanation of the use of ElementUI in Vue

Login + sessionStorage Effect display After a suc...

MySQL 5.7.27 installation and configuration method graphic tutorial

MySQL 5.7.27 detailed download, installation and ...

Complete example of Vue encapsulating the global toast component

Table of contents Preface 1. With vue-cli 1. Defi...

How to resize partitions in CentOS7

Yesterday, I helped someone install a system and ...