hookehuyr

封装keepAlive缓存操作

1 +import { mainStore } from '@/utils/generatePackage.js'
2 +
3 +// 删除 keep-alive 缓存
4 +export const store = mainStore();
5 +
6 +export const killPages = () => {
7 + store.changeKeepPages();
8 +}
9 +
10 +export const addPages = () => {
11 + store.keepThisPage();
12 +}
...@@ -44,18 +44,18 @@ ...@@ -44,18 +44,18 @@
44 </template> 44 </template>
45 45
46 <script setup> 46 <script setup>
47 -import { _, mainStore } from '@/utils/generatePackage' 47 +import { _ } from '@/utils/generatePackage'
48 import { no_image, icon_avatar } from '@/utils/generateIcons' 48 import { no_image, icon_avatar } from '@/utils/generateIcons'
49 import { MyButton, ShortcutFixed, BookCard } from '@/utils/generateModules' 49 import { MyButton, ShortcutFixed, BookCard } from '@/utils/generateModules'
50 import { styleObject3 } from '@/settings/designSetting.js' 50 import { styleObject3 } from '@/settings/designSetting.js'
51 import { useBookList, useShortcutBar } from '@/composables'; 51 import { useBookList, useShortcutBar } from '@/composables';
52 import { useGo } from '@/hooks/useGo' 52 import { useGo } from '@/hooks/useGo'
53 +import { killPages, store } from '@/hooks/useKeepAlive'
53 54
54 const go = useGo() 55 const go = useGo()
55 56
56 // 删除所有的 keep-alive 缓存 57 // 删除所有的 keep-alive 缓存
57 -const store = mainStore() 58 +killPages();
58 -store.changeKeepPages();
59 // 清空记录位置 59 // 清空记录位置
60 store.changeScrollTop(0); 60 store.changeScrollTop(0);
61 61
......
...@@ -15,14 +15,13 @@ ...@@ -15,14 +15,13 @@
15 </template> 15 </template>
16 16
17 <script setup> 17 <script setup>
18 -import { mainStore } from '@/store'
19 import RightSideList from '@/components/RightSideList/index' 18 import RightSideList from '@/components/RightSideList/index'
20 import { useGo } from '@/hooks/useGo' 19 import { useGo } from '@/hooks/useGo'
20 +import { killPages } from '@/hooks/useKeepAlive'
21 21
22 const go = useGo() 22 const go = useGo()
23 // 删除所有的 keep-alive 缓存 23 // 删除所有的 keep-alive 缓存
24 -const store = mainStore() 24 +killPages();
25 -store.changeKeepPages();
26 </script> 25 </script>
27 26
28 <script> 27 <script>
......
...@@ -19,7 +19,6 @@ ...@@ -19,7 +19,6 @@
19 </template> 19 </template>
20 20
21 <script setup> 21 <script setup>
22 -import { mainStore } from '@/store'
23 import Cookies from 'js-cookie' 22 import Cookies from 'js-cookie'
24 import 'animate.css'; 23 import 'animate.css';
25 import MyButton from '@/components/MyButton/index.vue' 24 import MyButton from '@/components/MyButton/index.vue'
...@@ -27,13 +26,13 @@ import { onMounted } from 'vue' ...@@ -27,13 +26,13 @@ import { onMounted } from 'vue'
27 import { useGo, useReplace } from '@/hooks/useGo' 26 import { useGo, useReplace } from '@/hooks/useGo'
28 // 自定义按钮颜色样式 27 // 自定义按钮颜色样式
29 import { styleObject1, styleObject2 } from '@/settings/designSetting.js' 28 import { styleObject1, styleObject2 } from '@/settings/designSetting.js'
29 +import { addPages } from '@/hooks/useKeepAlive'
30 30
31 const go = useGo(); 31 const go = useGo();
32 const replace = useReplace(); 32 const replace = useReplace();
33 33
34 // TAG: keepAlive 缓存页面 34 // TAG: keepAlive 缓存页面
35 -const store = mainStore(); 35 +addPages();
36 -store.keepThisPage();
37 36
38 onMounted(() => { 37 onMounted(() => {
39 // 判断微信授权状态,进入页面时未授权需要授权跳转 38 // 判断微信授权状态,进入页面时未授权需要授权跳转
......
...@@ -59,22 +59,19 @@ ...@@ -59,22 +59,19 @@
59 </template> 59 </template>
60 60
61 <script setup> 61 <script setup>
62 -import { mainStore } from '@/store'
63 import { storeToRefs } from 'pinia' 62 import { storeToRefs } from 'pinia'
64 -
65 import VideoCard from '@/components/VideoCard/index.vue' 63 import VideoCard from '@/components/VideoCard/index.vue'
66 import icon_avatar from '@images/que-touxiang@2x.png' 64 import icon_avatar from '@images/que-touxiang@2x.png'
67 import no_image from '@images/que-shuju@2x.png' 65 import no_image from '@images/que-shuju@2x.png'
68 - 66 +import { ref, onActivated } from 'vue'
69 -import { ref, onActivated, onMounted } from 'vue' 67 +import { useRoute, onBeforeRouteLeave } from 'vue-router'
70 -import { useRoute, useRouter, onBeforeRouteLeave } from 'vue-router'
71 import axios from '@/utils/axios'; 68 import axios from '@/utils/axios';
72 import _ from 'lodash'; 69 import _ from 'lodash';
73 import $ from 'jquery' 70 import $ from 'jquery'
74 import { Toast } from 'vant'; 71 import { Toast } from 'vant';
72 +import { addPages, store } from '@/hooks/useKeepAlive'
75 73
76 const $route = useRoute(); 74 const $route = useRoute();
77 -const $router = useRouter();
78 75
79 const userInfo = ref({}); 76 const userInfo = ref({});
80 // 因为不能让空图标提前出来的写法 77 // 因为不能让空图标提前出来的写法
...@@ -138,8 +135,7 @@ const followUser = (status) => { ...@@ -138,8 +135,7 @@ const followUser = (status) => {
138 /****************** keepAlive 模块 *******************/ 135 /****************** keepAlive 模块 *******************/
139 136
140 // TAG: keepAlive 缓存页面 137 // TAG: keepAlive 缓存页面
141 -const store = mainStore(); 138 +addPages()
142 -store.keepThisPage($route.meta.name);
143 const scrollY = ref(0) 139 const scrollY = ref(0)
144 140
145 onActivated(() => { // keepAlive 重置后执行回调 141 onActivated(() => { // keepAlive 重置后执行回调
......
...@@ -64,6 +64,7 @@ import { useRoute, useRouter } from 'vue-router' ...@@ -64,6 +64,7 @@ import { useRoute, useRouter } from 'vue-router'
64 import axios from '@/utils/axios'; 64 import axios from '@/utils/axios';
65 import _ from 'lodash' 65 import _ from 'lodash'
66 import { Toast } from 'vant'; 66 import { Toast } from 'vant';
67 +import { addPages, store } from '@/hooks/useKeepAlive'
67 68
68 const $route = useRoute(); 69 const $route = useRoute();
69 const $router = useRouter(); 70 const $router = useRouter();
...@@ -207,8 +208,7 @@ const onClick = (item) => { ...@@ -207,8 +208,7 @@ const onClick = (item) => {
207 /****************** keepAlive 模块 *******************/ 208 /****************** keepAlive 模块 *******************/
208 209
209 // TAG: keepAlive 缓存页面 210 // TAG: keepAlive 缓存页面
210 -const store = mainStore(); 211 +addPages();
211 -store.keepThisPage($route.meta.name);
212 212
213 onActivated(() => { // keepAlive 重置后执行回调 213 onActivated(() => { // keepAlive 重置后执行回调
214 }); 214 });
......
...@@ -87,9 +87,9 @@ ...@@ -87,9 +87,9 @@
87 </template> 87 </template>
88 88
89 <script setup> 89 <script setup>
90 -import { ref, reactive, onMounted } from 'vue' 90 +import { ref, onMounted } from 'vue'
91 import { useRouter } from 'vue-router' 91 import { useRouter } from 'vue-router'
92 -import { _, mainStore } from '@/utils/generatePackage.js' 92 +import { _ } from '@/utils/generatePackage.js'
93 import { MyButton, NoticeOverlay } from '@/utils/generateModules.js' 93 import { MyButton, NoticeOverlay } from '@/utils/generateModules.js'
94 import { icon_avatar } from '@/utils/generateIcons.js' 94 import { icon_avatar } from '@/utils/generateIcons.js'
95 import { changePerformerAPI, myInfoAPI, myPerformerAPI } from '@/api/C/me.js' 95 import { changePerformerAPI, myInfoAPI, myPerformerAPI } from '@/api/C/me.js'
...@@ -97,13 +97,13 @@ import meRoute from '@/router/routes/modules/me' ...@@ -97,13 +97,13 @@ import meRoute from '@/router/routes/modules/me'
97 // 自定义按钮颜色样式 97 // 自定义按钮颜色样式
98 import { styleObject1, styleObject4, styleObject5, styleObject6 } from '@/settings/designSetting.js' 98 import { styleObject1, styleObject4, styleObject5, styleObject6 } from '@/settings/designSetting.js'
99 import { useGo } from '@/hooks/useGo' 99 import { useGo } from '@/hooks/useGo'
100 +import { killPages } from '@/hooks/useKeepAlive'
100 101
101 const go = useGo(); 102 const go = useGo();
102 const $router = useRouter(); 103 const $router = useRouter();
103 104
104 // 删除 keep-alive 缓存 105 // 删除 keep-alive 缓存
105 -const store = mainStore(); 106 +killPages();
106 -store.changeKeepPages('clear');
107 107
108 // onBeforeRouteLeave(() => { 108 // onBeforeRouteLeave(() => {
109 // 重置位置缓存 109 // 重置位置缓存
......
...@@ -60,6 +60,7 @@ import { useRoute, useRouter, onBeforeRouteLeave } from 'vue-router' ...@@ -60,6 +60,7 @@ import { useRoute, useRouter, onBeforeRouteLeave } from 'vue-router'
60 import axios from '@/utils/axios'; 60 import axios from '@/utils/axios';
61 import _ from 'lodash' 61 import _ from 'lodash'
62 import { Toast, Dialog } from 'vant'; 62 import { Toast, Dialog } from 'vant';
63 +import { addPages, store } from '@/hooks/useKeepAlive'
63 64
64 const $route = useRoute(); 65 const $route = useRoute();
65 const $router = useRouter(); 66 const $router = useRouter();
...@@ -161,8 +162,7 @@ const deleteComment = (item) => { // 删除评论 ...@@ -161,8 +162,7 @@ const deleteComment = (item) => { // 删除评论
161 /****************** keepAlive 模块 *******************/ 162 /****************** keepAlive 模块 *******************/
162 163
163 // TAG: keepAlive 缓存页面 164 // TAG: keepAlive 缓存页面
164 -const store = mainStore(); 165 +addPages()
165 -store.keepThisPage($route.meta.name);
166 166
167 onActivated(() => { // keepAlive 重置后执行回调 167 onActivated(() => { // keepAlive 重置后执行回调
168 }); 168 });
......
...@@ -30,18 +30,14 @@ import { ref, onActivated, computed } from 'vue' ...@@ -30,18 +30,14 @@ import { ref, onActivated, computed } from 'vue'
30 import { useRoute, useRouter, onBeforeRouteLeave } from 'vue-router' 30 import { useRoute, useRouter, onBeforeRouteLeave } from 'vue-router'
31 import $ from 'jquery' 31 import $ from 'jquery'
32 import _ from 'lodash'; 32 import _ from 'lodash';
33 -import { wxInfo } from '@/utils/tools'; 33 +import { addPages, store } from '@/hooks/useKeepAlive'
34 -
35 -const $route = useRoute();
36 -const $router = useRouter();
37 34
38 const { onLoad, prod_list, finished, loading, finishedTextStatus, emptyStatus } = useUnwatchList(); 35 const { onLoad, prod_list, finished, loading, finishedTextStatus, emptyStatus } = useUnwatchList();
39 36
40 /****************** keepAlive 模块 *******************/ 37 /****************** keepAlive 模块 *******************/
41 38
42 // TAG: keepAlive 缓存页面 39 // TAG: keepAlive 缓存页面
43 -const store = mainStore(); 40 +addPages()
44 -store.keepThisPage($route.meta.name);
45 41
46 onActivated(() => { // keepAlive 重置后执行回调 42 onActivated(() => { // keepAlive 重置后执行回调
47 // TAG: pinia应用,动态刷新数据 43 // TAG: pinia应用,动态刷新数据
......