Specific steps for Vue browser to return monitoring

Specific steps for Vue browser to return monitoring

Preface

When sharing a page, you hope to click the browser back button to return to the project homepage to increase the number of visits.

You need to listen to the browser's back button and prevent its default event.

The specific steps are as follows:

1. After mounting is completed, determine whether the browser supports popstate

mounted(){
 if (window.history && window.history.pushState) {
 history.pushState(null, null, document.URL);
 window.addEventListener('popstate', this.goBack, false);
 }
},

2. When the page is destroyed, cancel the monitoring. Otherwise other vue routing pages will also be monitored

destroyed()
 window.removeEventListener('popstate', this.goBack, false);
},

3. Write the monitoring operation in methods. The content of removeEventListener to cancel the monitoring must be consistent with the content of opening the monitoring, so the function is written in methods.

methods:{
 goBack(){
 this.$router.replace({path: '/'});
 //replace replaces the original route to avoid falling back to an infinite loop}
}

P.S. What is popstate used for?

How to use popstate?

The new HTML5 API extends window.history to make history points more open. You can store the current history point pushState, replace the current history point replaceState, and listen to the history point popstate.

The usage of pushState and replaceState are similar.

Directions:

history.pushState(data,title,url);
//The first parameter data is the value of state; the second parameter title is the title of the page, but all current browsers ignore this parameter, so just pass an empty string; the third parameter url is the link you want to go to;

The usage of replaceState is similar, for example: history.replaceState("首頁","",location.href+ "#news");

Summarize

This is the end of this article about vue browser return monitoring. For more relevant vue browser return monitoring 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:
  • Problem of listening to the return key in Vue
  • When Vue pops up, listen to the phone's return key to close the pop-up function (the page does not jump)
  • Implementation of monitoring the physical return button of the mobile phone in the Vue project
  • Vue listens to the browser's native return button to perform route jump operations
  • Vue routing cache routing nested routing guard listens to physical return operations

<<:  Detailed Tutorial on Installing MySQL 5.7 on RedHat 6.5

>>:  How to use dd command in Linux without destroying the disk

Recommend

Implementation of MySQL scheduled database backup (full database backup)

Table of contents 1. MySQL data backup 1.1, mysql...

Example of how to set up a third-level domain name in nginx

Problem Description By configuring nginx, you can...

Detailed explanation of how to use Docker-Compose commands

You can manage and deploy Docker containers in a ...

How to view the execution time of SQL statements in MySQL

Table of contents 1. Initial SQL Preparation 2. M...

Use href in html to pop up a file download dialog box when clicking a link

I learned a new trick today. I didn’t know it befo...

Detailed explanation of JDBC database link and related method encapsulation

Detailed explanation of JDBC database link and re...

MySQL query example explanation through instantiated object parameters

This article will introduce how to query data in ...

Implementation of installing Docker in win10 environment

1. Enter the Docker official website First, go to...

VMware Tools installation and configuration tutorial for Ubuntu

Some time ago, the blogger installed the Ubuntu s...

Three.js sample code for implementing dewdrop animation effect

Preface Hello everyone, this is the CSS wizard - ...

vue2.x configuration from vue.config.js to project optimization

Table of contents Preface vue.config.js configura...