1 | import Vue from "nativescript-vue";
|
2 | import { Page } from "@nativescript/core/ui/page";
|
3 | export default {
|
4 | nextCallbacks: [],
|
5 | mounted() {
|
6 | if (!this.nativeView.eventsSet && this.isPage()) {
|
7 | this.nativeView.eventsSet = true;
|
8 | this.nativeView.on(Page.navigatingFromEvent, this.onNavigatingFrom.bind(this));
|
9 | this.nativeView.on(Page.navigatingToEvent, this.onNavigatingTo.bind(this));
|
10 | this.nativeView.on(Page.navigatedToEvent, this.onNavigatedTo.bind(this));
|
11 | this.nativeView.on(Page.navigatedFromEvent, this.onNavigatedFrom.bind(this));
|
12 | }
|
13 | },
|
14 | destroyed() {
|
15 | if (this.nativeView.eventsSet && this.isPage()) {
|
16 | this.nativeView.off(Page.navigatedToEvent, this.onNavigatedTo.bind(this));
|
17 | this.nativeView.off(Page.navigatingToEvent, this.onNavigatingTo.bind(this));
|
18 | this.nativeView.off(Page.navigatingFromEvent, this.onNavigatingFrom.bind(this));
|
19 | this.nativeView.off(Page.navigatedFromEvent, this.onNavigatedFrom.bind(this));
|
20 | }
|
21 | },
|
22 | methods: {
|
23 | onNavigatingFrom(data) {
|
24 | if (data.object.frame &&
|
25 | data.object.frame.parent &&
|
26 | data.object.frame.parent.modal) {
|
27 | Vue.prototype.$modalPage = {
|
28 | data,
|
29 | instance: this,
|
30 | };
|
31 | return;
|
32 | }
|
33 | const to = this.$router.getNewRoute();
|
34 | const from = this.$router.getCurrentRoute();
|
35 | if (this.$options.beforeRouteLeave) {
|
36 | this.$options.beforeRouteLeave.call(this, to, from, data.object.navigationContext);
|
37 | }
|
38 | if (this.$options.navigatingFrom) {
|
39 | console.warn("ROUTER", "navigatingFrom() is deprecated. Use beforeRouteLeave() instead");
|
40 | this.$options.navigatingFrom.call(this, to, from, data.object.navigationContext);
|
41 | }
|
42 | },
|
43 | onNavigatingTo(data) {
|
44 | const to = this.$router.getNewRoute();
|
45 | const from = this.$router.getCurrentRoute();
|
46 | if (this.$options.beforeRouteUpdate && to.path === from.path) {
|
47 | this.$options.beforeRouteUpdate.call(this, to, from, data.object.navigationContext);
|
48 | }
|
49 | if (this.$options.beforeRouteEnter) {
|
50 | const next = (vmContext) => {
|
51 | if (typeof vmContext === "undefined") {
|
52 | return;
|
53 | }
|
54 | if (typeof vmContext === "function") {
|
55 | this.$options.nextCallbacks.push(vmContext);
|
56 | }
|
57 | };
|
58 | this.$options.beforeRouteEnter.call(this, to, from, next, data.object.navigationContext);
|
59 | }
|
60 | this.$router.invokeBeforeResolve();
|
61 | if (this.$options.navigatingTo) {
|
62 | console.warn("ROUTER", "navigatingTo() is deprecated. Use beforeRouteEnter() instead");
|
63 | this.$options.navigatingTo.call(this, to, from, data.object.navigationContext);
|
64 | }
|
65 | },
|
66 | onNavigatedFrom(data) {
|
67 | if (this.$options.navigatedFrom) {
|
68 | console.warn("ROUTER", "navigatedFrom() is deprecated. For store state updates use meta dispatcher instead. For component state you could opt for beforeRouteLeave()");
|
69 | this.$options.navigatedFrom.call(this, data.object.navigationContext);
|
70 | }
|
71 | },
|
72 | onNavigatedTo(data) {
|
73 | this.$router.invokeAfterEach();
|
74 | this.$options.nextCallbacks.forEach((callback) => {
|
75 | callback.call(this, this, data.object.navigationContext);
|
76 | });
|
77 | this.$options.nextCallbacks.splice(0);
|
78 | if (this.$options.navigatedTo) {
|
79 | console.warn("ROUTER", 'navigatedTo() is deprecated. Use beforeRouteEnter() with "next(vm => ...)" callback instead');
|
80 | this.$options.navigatedTo.call(this, data.object.navigationContext);
|
81 | }
|
82 | },
|
83 | isPage() {
|
84 | if (this.nativeView.__vuePageRef__) {
|
85 | return true;
|
86 | }
|
87 | return false;
|
88 | },
|
89 | },
|
90 | };
|
91 |
|
\ | No newline at end of file |