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: 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:
|
<<: Detailed Tutorial on Installing MySQL 5.7 on RedHat 6.5
>>: How to use dd command in Linux without destroying the disk
Table of contents 1. MySQL data backup 1.1, mysql...
An absolute URL is used to represent all the conte...
Problem Description By configuring nginx, you can...
You can manage and deploy Docker containers in a ...
Table of contents 1. JavaScript uses canvas in HT...
Table of contents 1. Initial SQL Preparation 2. M...
I learned a new trick today. I didn’t know it befo...
Table of contents Step 1: Update Packages on Cent...
Detailed explanation of JDBC database link and re...
This article will introduce how to query data in ...
1. Enter the Docker official website First, go to...
There is currently a requirement that an operatio...
Some time ago, the blogger installed the Ubuntu s...
Preface Hello everyone, this is the CSS wizard - ...
Table of contents Preface vue.config.js configura...