hookehuyr

✨ feat(下拉加载页面): keepAlive缓存相应页面

1 <template> 1 <template>
2 - <router-view></router-view> 2 + <!-- 页面缓存 -->
3 + <router-view v-slot="{ Component }">
4 + <keep-alive>
5 + <component :is="Component" :key="$route.name" v-if="$route.meta.keepAlive"/>
6 + </keep-alive>
7 + <component :is="Component" :key="$route.name" v-if="!$route.meta.keepAlive"/>
8 + </router-view>
3 </template> 9 </template>
4 10
5 <script setup> 11 <script setup>
...@@ -37,6 +43,32 @@ if (!Cookies.get('default_perf')) { ...@@ -37,6 +43,32 @@ if (!Cookies.get('default_perf')) {
37 } 43 }
38 </script> 44 </script>
39 45
46 +<script>
47 +import _ from 'lodash';
48 +
49 +export default {
50 + beforeRouteEnter(to, from, next) {
51 + // 如果是从下面页面返回,需要重置keepAlive状态
52 + const arr = ['/me/message', '/me/callMe', '/client/bookDetail'];
53 + if (_.findIndex(arr, (path) => path === from.path) !== -1) {
54 + from.meta.keepAlive = false;
55 + }
56 + next();
57 + },
58 + data() {
59 + return {
60 +
61 + }
62 + },
63 + mounted() {
64 +
65 + },
66 + methods: {
67 +
68 + }
69 +}
70 +</script>
71 +
40 <style lang="less"> 72 <style lang="less">
41 html, 73 html,
42 body { 74 body {
......
...@@ -35,7 +35,8 @@ export default [{ ...@@ -35,7 +35,8 @@ export default [{
35 name: '客户端选择书籍下视频作品', 35 name: '客户端选择书籍下视频作品',
36 component: () => import('./views/client/bookDetail.vue'), 36 component: () => import('./views/client/bookDetail.vue'),
37 meta: { 37 meta: {
38 - title: '书籍' 38 + title: '书籍',
39 + keepAlive: true
39 }, 40 },
40 children: [] 41 children: []
41 }, { 42 }, {
...@@ -169,7 +170,8 @@ export default [{ ...@@ -169,7 +170,8 @@ export default [{
169 name: '我的留言', 170 name: '我的留言',
170 component: () => import('./views/me/message.vue'), 171 component: () => import('./views/me/message.vue'),
171 meta: { 172 meta: {
172 - title: '我的留言' 173 + title: '我的留言',
174 + keepAlive: true
173 }, 175 },
174 children: [] 176 children: []
175 }, { 177 }, {
...@@ -177,7 +179,8 @@ export default [{ ...@@ -177,7 +179,8 @@ export default [{
177 name: '@我的', 179 name: '@我的',
178 component: () => import('./views/me/callMe.vue'), 180 component: () => import('./views/me/callMe.vue'),
179 meta: { 181 meta: {
180 - title: '@我的' 182 + title: '@我的',
183 + keepAlive: true
181 }, 184 },
182 children: [] 185 children: []
183 }, { 186 }, {
...@@ -251,4 +254,4 @@ export default [{ ...@@ -251,4 +254,4 @@ export default [{
251 meta: { 254 meta: {
252 title: '' 255 title: ''
253 } 256 }
254 -}];
...\ No newline at end of file ...\ No newline at end of file
257 +}];
......
...@@ -129,9 +129,9 @@ const sendCode = () => { // 发送验证码 ...@@ -129,9 +129,9 @@ const sendCode = () => { // 发送验证码
129 }) 129 })
130 } 130 }
131 }; 131 };
132 - 132 +// TEMP:测试数据
133 -const phone = ref(''); 133 +const phone = ref('13812345678');
134 -const code = ref(''); 134 +const code = ref('8888');
135 const disabled = ref(true); 135 const disabled = ref(true);
136 // 过滤输入的数字 只能四位 136 // 过滤输入的数字 只能四位
137 const formatter = (value) => value.substring(0, 4); 137 const formatter = (value) => value.substring(0, 4);
...@@ -235,4 +235,4 @@ body { ...@@ -235,4 +235,4 @@ body {
235 border-radius: 5px; 235 border-radius: 5px;
236 } 236 }
237 } 237 }
238 -</style>
...\ No newline at end of file ...\ No newline at end of file
238 +</style>
......
...@@ -113,7 +113,7 @@ import icon_unsubscribe from '@images/icon-dingyue02@2x.png' ...@@ -113,7 +113,7 @@ import icon_unsubscribe from '@images/icon-dingyue02@2x.png'
113 113
114 import ShortcutFixed from '@/components/ShortcutFixed/index.vue' 114 import ShortcutFixed from '@/components/ShortcutFixed/index.vue'
115 import tools from '@/common/tool' 115 import tools from '@/common/tool'
116 -import { ref, reactive, onMounted, nextTick } from 'vue' 116 +import { ref, reactive, onMounted, nextTick, onActivated } from 'vue'
117 import { useRoute, useRouter } from 'vue-router' 117 import { useRoute, useRouter } from 'vue-router'
118 import axios from '@/utils/axios'; 118 import axios from '@/utils/axios';
119 import $ from 'jquery' 119 import $ from 'jquery'
...@@ -243,6 +243,10 @@ const uploadVideo = () => { ...@@ -243,6 +243,10 @@ const uploadVideo = () => {
243 console.error(err); 243 console.error(err);
244 }) 244 })
245 } 245 }
246 +
247 +onActivated(() => { // keepAlive重置后执行回调
248 + onLoad()
249 +});
246 </script> 250 </script>
247 251
248 <script> 252 <script>
......
...@@ -54,7 +54,7 @@ ...@@ -54,7 +54,7 @@
54 import CommentBox from '@/components/CommentBox/index.vue' 54 import CommentBox from '@/components/CommentBox/index.vue'
55 import NoticeOverlay from '@/components/NoticeOverlay/index.vue' 55 import NoticeOverlay from '@/components/NoticeOverlay/index.vue'
56 56
57 -import { ref, reactive, onMounted } from 'vue' 57 +import { ref, reactive, onActivated } from 'vue'
58 import { useRoute, useRouter } from 'vue-router' 58 import { useRoute, useRouter } from 'vue-router'
59 import axios from '@/utils/axios'; 59 import axios from '@/utils/axios';
60 import _ from 'lodash' 60 import _ from 'lodash'
...@@ -201,9 +201,9 @@ const onClick = (item) => { ...@@ -201,9 +201,9 @@ const onClick = (item) => {
201 }); 201 });
202 } 202 }
203 203
204 - onMounted(() => { 204 +onActivated(() => { // keepAlive重置后执行回调
205 - 205 + onLoad()
206 - }) 206 +});
207 </script> 207 </script>
208 208
209 <script> 209 <script>
...@@ -243,4 +243,4 @@ export default { ...@@ -243,4 +243,4 @@ export default {
243 } 243 }
244 } 244 }
245 } 245 }
246 -</style>
...\ No newline at end of file ...\ No newline at end of file
246 +</style>
......
...@@ -286,6 +286,12 @@ const getUnwatch = () => { ...@@ -286,6 +286,12 @@ const getUnwatch = () => {
286 import mixin from 'common/mixin'; 286 import mixin from 'common/mixin';
287 287
288 export default { 288 export default {
289 + beforeRouteEnter(to, from, next) {
290 + if (from.path === '/me/message' || from.path === '/me/callMe') { // 如果是从[我的留言,@me]页面返回,需要重置keepAlive状态
291 + from.meta.keepAlive = false;
292 + }
293 + next();
294 + },
289 mixins: [mixin.init], 295 mixins: [mixin.init],
290 data() { 296 data() {
291 return { 297 return {
......
...@@ -45,7 +45,7 @@ ...@@ -45,7 +45,7 @@
45 <script setup> 45 <script setup>
46 import icon_player from '@images/bofang@2x.png' 46 import icon_player from '@images/bofang@2x.png'
47 47
48 -import { ref, reactive, onMounted } from 'vue' 48 +import { ref, reactive, onMounted, onActivated } from 'vue'
49 import { useRoute, useRouter } from 'vue-router' 49 import { useRoute, useRouter } from 'vue-router'
50 import axios from '@/utils/axios'; 50 import axios from '@/utils/axios';
51 import _ from 'lodash' 51 import _ from 'lodash'
...@@ -75,6 +75,7 @@ const onLoad = () => { ...@@ -75,6 +75,7 @@ const onLoad = () => {
75 item.c_name = arr[1]; // 评论姓名 75 item.c_name = arr[1]; // 评论姓名
76 }) 76 })
77 commentList.value = _.concat(commentList.value, res.data.data); 77 commentList.value = _.concat(commentList.value, res.data.data);
78 + commentList.value = _.uniqBy(commentList.value, 'id');
78 offset.value = commentList.value.length; 79 offset.value = commentList.value.length;
79 loading.value = false; 80 loading.value = false;
80 // 数据全部加载完成 81 // 数据全部加载完成
...@@ -135,6 +136,10 @@ const deleteComment = (item) => { // 删除评论 ...@@ -135,6 +136,10 @@ const deleteComment = (item) => { // 删除评论
135 // on cancel 136 // on cancel
136 }); 137 });
137 } 138 }
139 +
140 +onActivated(() => { // keepAlive重置后执行回调
141 + onLoad()
142 +});
138 </script> 143 </script>
139 144
140 <script> 145 <script>
...@@ -185,4 +190,4 @@ export default { ...@@ -185,4 +190,4 @@ export default {
185 margin-left: 1rem; 190 margin-left: 1rem;
186 } 191 }
187 } 192 }
188 -</style>
...\ No newline at end of file ...\ No newline at end of file
193 +</style>
......