-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathNavigatorPinia.vue
More file actions
107 lines (100 loc) · 2.59 KB
/
NavigatorPinia.vue
File metadata and controls
107 lines (100 loc) · 2.59 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<template>
<v-ons-page>
<v-ons-navigator swipeable v-model:page-stack="pageStack"></v-ons-navigator>
</v-ons-page>
</template>
<script>
import { markRaw } from 'vue';
import { defineStore, mapActions, mapWritableState } from 'pinia';
const usePageStackStore = defineStore('pageStack', {
state: () => ({ pageStack: [] }),
actions: {
pushPage(page) {
if (page instanceof Array) {
this.pageStack = [...this.pageStack, ...page.map(markRaw)];
} else {
this.pageStack = [...this.pageStack, markRaw(page)];
}
},
popPage() {
if (this.pageStack.length > 1) {
this.pageStack = this.pageStack.slice(0, -1);
}
},
replacePage(page) {
this.pageStack = [...this.pageStack.slice(0, -1), markRaw(page)]
},
resetPageStack() {
this.pageStack = [this.pageStack[0]];
}
}
});
const myToolbar = {
template: `
<v-ons-toolbar>
<div class="left"><v-ons-back-button>Back</v-ons-back-button></div>,
<div class="center"><slot></slot></div>
</v-ons-toolbar>
`
};
const page3 = {
template: `
<v-ons-page p3>
<my-toolbar>Page 3</my-toolbar>
Page 3
<v-ons-button @click="replace">Replace with first page</v-ons-button>
<v-ons-button @click="resetPageStack">Reset to first page</v-ons-button>
</v-ons-page>
`,
methods: {
...mapActions(usePageStackStore, ['replacePage', 'resetPageStack']),
replace() {
this.replacePage(page1);
}
},
components: { myToolbar }
};
const page2 = {
template: `
<v-ons-page p2>
<my-toolbar>Page 2</my-toolbar>
Page 2
<v-ons-button @click="push">Push 3 pages</v-ons-button>
</v-ons-page>
`,
methods: {
...mapActions(usePageStackStore, ['pushPage']),
push() {
this.pushPage([page3, page3, page3]);
}
},
components: { myToolbar }
};
const page1 = {
template: `
<v-ons-page p1>
<my-toolbar>Page 1</my-toolbar>
Page 1
<v-ons-button @click="push">Push</v-ons-button>
</v-ons-page>
`,
methods: {
...mapActions(usePageStackStore, ['pushPage']),
push() {
this.pushPage(page2);
}
},
components: { myToolbar }
};
export default {
computed: {
...mapWritableState(usePageStackStore, ['pageStack'])
},
beforeMount() {
this.pushPage(page1);
},
methods: {
...mapActions(usePageStackStore, ['pushPage', 'popPage', 'replacePage', 'resetPageStack']),
}
};
</script>