Home Web Front-end Front-end Q&A vue router delete history

vue router delete history

May 24, 2023 pm 02:07 PM

In the process of developing single-page applications using Vue Router, we often need to allow users to clear the browser history. But Vue Router does not provide a built-in method to help us implement this function, so we need to find a way to implement it ourselves.

Method 1:

One method is to use a method called "replaceState" in Javascript, which can replace the current browser history entry with a new entry, thereby achieving Purpose of deleting history. We can use this method with Vue Router. The specific steps are as follows:

  1. First, we need to intercept all routing jump events in the guard of Vue Router, and then change the routing object to be jumped. The path information is saved.
router.beforeEach((to, from, next) => {
    sessionStorage.setItem('toPath', to.fullPath) // 保存即将跳转的路由对象的路径
    next()
})
Copy after login
  1. Then, when the user wants to clear the browser history, we can get the previously saved path from sessionStorage and then use the "replaceState" method to replace the current history with The history record of this path, so as to achieve the purpose of deleting the history record.
function clearHistory() {
    const toPath = sessionStorage.getItem('toPath')
    history.replaceState(null, '', toPath)
    sessionStorage.removeItem('toPath')
}
Copy after login
  1. Finally, we expose this method of clearing history for users to call.
export default {
    clearHistory
}
Copy after login

To summarize the steps of this method:

  1. Save the path of the routing object to be jumped to sessionStorage in the guard of Vue Router.
  2. When you need to clear the history, get the previously saved path from sessionStorage, and use the "replaceState" method to replace the current history with the history of that path.
  3. Expose an API interface for users to call the method of clearing history.

Method 2:

Another way to clear browser history is to use the hook function of Vue Router. The specific steps are as follows:

  1. We can use the "replace" method in the global post-hook function of Vue Router to replace the current routing path with the previous routing path, thereby achieving the purpose of deleting the history record.
router.afterEach((to, from) => {
    if (!sessionStorage.getItem('isBack')) {
        history.replaceState(null, '', from.fullPath)
        sessionStorage.setItem('fromPath', from.fullPath) // 保存从哪个路由页面来
    }
    sessionStorage.removeItem('isBack') // 操作完后,清除标识变量
})
Copy after login
  1. Then, we can trigger the event of deleting the history record in the component. The specific implementation can use Vue's $emit method to pass data to the parent component.
this.$emit('clearHistory')
Copy after login
  1. Listen to the event of deleting history records in the parent component, call the "replace" method on the routing object in the callback function, and replace the path of the current routing object with the previous path. This will enable you to clear your browser history.
<template>
    <button @click="handleClearHistory">清除历史记录</button>
</template>
<script>
export default {
    methods: {
        handleClearHistory() {
            this.$router.replace(sessionStorage.getItem('fromPath'))
            sessionStorage.setItem('isBack', 'true')
        }
    }
}
</script>
Copy after login

Summarize the steps of this method:

  1. In the global post-hook function of Vue Router, save the routing path of the current page to sessionStorage.
  2. Trigger the event of deleting the history record in the component that needs to delete the history record, and use the $emit method to pass the data to the parent component.
  3. Listen to the event of deleting the history record in the parent component, and call the "replace" method on the routing object in the callback function to replace the path of the current routing object with the previous path.

To sum up, we can use either of these two methods to achieve the function of deleting browser history. Which method to choose can be based on specific business needs and development scene to determine. Hope this article is helpful to you.

The above is the detailed content of vue router delete history. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is useEffect? How do you use it to perform side effects? What is useEffect? How do you use it to perform side effects? Mar 19, 2025 pm 03:58 PM

What is useEffect? How do you use it to perform side effects?

Explain the concept of lazy loading. Explain the concept of lazy loading. Mar 13, 2025 pm 07:47 PM

Explain the concept of lazy loading.

How does currying work in JavaScript, and what are its benefits? How does currying work in JavaScript, and what are its benefits? Mar 18, 2025 pm 01:45 PM

How does currying work in JavaScript, and what are its benefits?

What are higher-order functions in JavaScript, and how can they be used to write more concise and reusable code? What are higher-order functions in JavaScript, and how can they be used to write more concise and reusable code? Mar 18, 2025 pm 01:44 PM

What are higher-order functions in JavaScript, and how can they be used to write more concise and reusable code?

How does the React reconciliation algorithm work? How does the React reconciliation algorithm work? Mar 18, 2025 pm 01:58 PM

How does the React reconciliation algorithm work?

What is useContext? How do you use it to share state between components? What is useContext? How do you use it to share state between components? Mar 19, 2025 pm 03:59 PM

What is useContext? How do you use it to share state between components?

How do you prevent default behavior in event handlers? How do you prevent default behavior in event handlers? Mar 19, 2025 pm 04:10 PM

How do you prevent default behavior in event handlers?

What are the advantages and disadvantages of controlled and uncontrolled components? What are the advantages and disadvantages of controlled and uncontrolled components? Mar 19, 2025 pm 04:16 PM

What are the advantages and disadvantages of controlled and uncontrolled components?

See all articles