刷新页面时重定向到另一个页面(F5)

Redirect to another page when refreshing the page (F5)

我有一个使用 browsRouter 执行转换的组件。我需要,当用户试图刷新页面时,将他重定向到登录页面,这怎么办?

您可以使用Navigation Timing API。把这个放在你的组件构造函数中:

if (window.performance && window.performance.navigation.type == 1) {
    // Redirect to login page
}

更新 2022

下一个代码已弃用

window.performance.navigation.type

现在使用 getEntriesByType:

const entries = window.performance.getEntriesByType("navigation");

if (entries[0].type === "reload") {
    // Redirect to login page
}