4

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

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

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

3 回答 3

5

不是 Angular 特有的,但是这个 API 已经被PerformanceNavigationTiming替代了,它也有一个type属性,但是它返回一个字符串而不是一个数字代码。

但是我只是注意到 Chrome 不会为 iframe 公开这个,它总是会输出"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>

于 2019-11-01T01:38:47.950 回答
5

performance.navigation已弃用。要获取导航类型,您可以使用getEntriesByType

例子

console.log(performance.getEntriesByType("navigation")[0].type)

type值可以是

enum NavigationType {
    "navigate",
    "reload",
    "back_forward",
    "prerender"
};

查看更多:https ://w3c.github.io/navigation-timing/#sec-performance-navigation-types

于 2021-02-10T02:41:57.940 回答
3

我想添加运行:

performance.getEntriesByType("navigation");

在 Safari 中返回一个空数组

于 2021-05-26T18:50:24.900 回答