-
Notifications
You must be signed in to change notification settings - Fork 122
/
angular-devtools-extension.ts
55 lines (48 loc) · 1.78 KB
/
angular-devtools-extension.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { ReduxDevtoolsExtensionConfig } from "interfaces";
export class AngularDevToolsExtension {
private window = (window as any);
private router: any;
private ngZone: any;
constructor(private config?: ReduxDevtoolsExtensionConfig) {
// Angular with NO Ivy
if (this.window.ng && this.window.ng.probe && this.window.getAllAngularRootElements) {
const rootElements = this.window.ng.probe(this.window.getAllAngularRootElements()[0]);
const providers = rootElements.injector.view.root.ngModule._providers;
this.router = providers.find(p => p && p.constructor && p.constructor.name === 'Router');
try {
this.ngZone = rootElements.injector.get(this.window.ng.coreTokens.NgZone);
}
catch (e) {
console.log(e);
}
return;
}
// Angular with Ivy
if (this.window.ng && this.window.ng.getInjector && this.window.getAllAngularRootElements &&
this.config && this.config.router && this.config.ngZone) {
try {
const injector = this.window.ng.getInjector(this.window.getAllAngularRootElements()[0]);
this.router = injector.get(this.config.router);
this.ngZone = injector.get(this.config.ngZone);
}
catch (e) {
console.log(e);
}
return;
}
}
navigate(path: string) {
if (this.ngZone && this.router) {
this.runInZone(() => {
this.router.navigateByUrl(path);
});
}
}
runInZone(action: any) {
if (this.ngZone) {
this.ngZone.run(() => {
action();
});
}
}
}