本文介绍了angular 中 performance.navigation.type 的替代品是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码是为了知道页面是否被用户重新加载,不幸的是已被弃用.我想知道angular有没有这种方法.

I have this code in order to know if the page is reload by the user, unfortunately is deprecated. I want to know if angular has this approach.

if (performance.navigation.type === 1) {
   console.log('is refreshed')
}

if (performance.navigation.type === 0) {
   console.log('the user is arrived')
}

推荐答案

并非特定于 Angular,但此 API 已被 PerformanceNavigationTiming 一个,它还有一个 type 属性,但返回字符串而不是数字代码.

Not specific to Angular, but this API has been replaced by the PerformanceNavigationTiming one, which also has a type property, but which returns a string instead of a numerical code.

但是我刚刚注意到 Chrome 不会为 iframe 公开它,它总是输出 "navigate".

However I just noticed that Chrome won't expose this for iframes, which will always output "navigate".

以下代码段在 Chrome 中不起作用,请尝试这个 plnkr 代替,在外部视图中.

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

console.log( entries.map( nav => nav.type ) );

rel.onclick = e => location.reload();
<button type="button" id="rel">reload</button>
<a href="404">go to 404 (come back with your browser's back button)</a>

这篇关于angular 中 performance.navigation.type 的替代品是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 13:41
查看更多