检测是否在VUE中单击了“刷新”按钮

时间:2018-04-13 09:10:44

标签: javascript vuejs2

如果我们可以检查是否点击了浏览器的刷新按钮,按下了F5还是在Vue中点击了重新加载,有没有办法?

enter image description here enter image description here

赞赏任何意见或建议。

1 个答案:

答案 0 :(得分:2)

Navigation Timing API,特别是PerformanceNavigation.type,会告诉您网页的访问方式。

0 - TYPE_NAVIGATE

  

通过链接,书签,表单提交,脚本或在地址栏中键入URL来访问页面。

1 - TYPE_RELOAD

  

通过单击“重新加载”按钮或通过Location.reload()方法访问该页面。

2 - TYPE_BACK_FORWARD

  

通过导航到历史记录来访问该页面。

255 - TYPE_RESERVED

  

任何其他方式。

使用示例:

// does the browser support the Navigation Timing API?
if (window.performance) {
  console.info("window.performance is supported");
}

// do something based on the navigation type...
switch(performance.navigation.type) {
  case 0:
    console.info("TYPE_NAVIGATE");
    break;
  case 1:
    console.info("TYPE_RELOAD");
    break;
  case 2:
    console.info("TYPE_BACK_FORWARD");
    break;
  case 255:
    console.info("255");
    break;
}