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

Introduction to JWT Verification Using Nginx and Lua

Table of contents Preface Lua Script nignx.conf c...

Solution to mysql ERROR 1045 (28000) problem

I encountered mysql ERROR 1045 and spent a long t...

Implement group by based on MySQL to get the latest data of each group

Preface: The group by function retrieves the firs...

A brief discussion of four commonly used storage engines in MySQL

Introduction to four commonly used MySQL engines ...

CSS Houdini achieves dynamic wave effect

CSS Houdini is known as the most exciting innovat...

Detailed process of compiling and installing Storm on Kylin V10 server

1 Introduction Apache Storm is a free, open sourc...

Docker uses root to enter the container

First run the docker container Run the command as...

Specific use of Linux which command

We often want to find a file in Linux, but we don...

Installation and use of mysql on Ubuntu (general version)

Regardless of which version of Ubuntu, installing...

Lombok implementation JSR-269

Preface Introduction Lombok is a handy tool, just...

How to deploy nginx with Docker and modify the configuration file

Deploy nginx with docker, it's so simple Just...

Advantages and disadvantages of common MySQL storage engines

Table of contents View all storage engines InnoDB...

How to solve the problem of character set when logging in to Linux

Character set error always exists locale: Cannot ...

Sample code for implementing multi-application deployment using tomcat+nginx

Table of contents Multi-application deployment 1-...