angular 中 performance.navigation.type 的替代品是什么?

Tab*_*res 3 javascript router navigateurl angular

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

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

if (performance.navigation.type === 0) {
   console.log('the user is arrived')
}  
Run Code Online (Sandbox Code Playgroud)

Bin*_* Ho 16

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

例子

console.log(performance.getEntriesByType("navigation")[0].type)
Run Code Online (Sandbox Code Playgroud)

type值可以是

enum NavigationType {
    "navigate",
    "reload",
    "back_forward",
    "prerender"
};
Run Code Online (Sandbox Code Playgroud)

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


Kai*_*ido 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();
Run Code Online (Sandbox Code Playgroud)
<button type="button" id="rel">reload</button>
<a href="404">go to 404 (come back with your browser's back button)</a>
Run Code Online (Sandbox Code Playgroud)


小智 5

我想添加运行:

performance.getEntriesByType("navigation");
Run Code Online (Sandbox Code Playgroud)

在 Safari 中返回空数组