hookehuyr

✨ feat(下拉列表页): keepAlive控制组件生命周期

1 <template> 1 <template>
2 <!-- 页面缓存 --> 2 <!-- 页面缓存 -->
3 - <router-view v-slot="{ Component }"> 3 + <router-view v-slot="{ Component }">
4 - <keep-alive> 4 + <keep-alive>
5 - <component :is="Component" :key="$route.name" v-if="$route.meta.keepAlive"/> 5 + <component :is="Component" :key="$route.name" v-if="$route.meta.keepAlive" />
6 - </keep-alive> 6 + </keep-alive>
7 - <component :is="Component" :key="$route.name" v-if="!$route.meta.keepAlive"/> 7 + <component :is="Component" :key="$route.name" v-if="!$route.meta.keepAlive" />
8 </router-view> 8 </router-view>
9 </template> 9 </template>
10 10
...@@ -15,6 +15,7 @@ import axios from '@/utils/axios' ...@@ -15,6 +15,7 @@ import axios from '@/utils/axios'
15 import { useRoute, useRouter } from 'vue-router' 15 import { useRoute, useRouter } from 'vue-router'
16 import { onMounted } from 'vue' 16 import { onMounted } from 'vue'
17 import { Toast } from 'vant' 17 import { Toast } from 'vant'
18 +// import _ from 'lodash';
18 import { mainStore } from './store'; 19 import { mainStore } from './store';
19 20
20 const store = mainStore(); 21 const store = mainStore();
...@@ -26,46 +27,20 @@ const $router = useRouter(); ...@@ -26,46 +27,20 @@ const $router = useRouter();
26 */ 27 */
27 if (!Cookies.get('default_perf')) { 28 if (!Cookies.get('default_perf')) {
28 axios.get('/srv/?a=default_perf') 29 axios.get('/srv/?a=default_perf')
29 - .then(res => { 30 + .then(res => {
30 - if (res.data.code === 1) { 31 + if (res.data.code === 1) {
31 - Cookies.set('default_perf', JSON.stringify(res.data.data)); 32 + Cookies.set('default_perf', JSON.stringify(res.data.data));
32 - } else { 33 + } else {
33 - console.warn(res); 34 + console.warn(res);
34 - Toast({ 35 + Toast({
35 - icon: 'close', 36 + icon: 'close',
36 - message: res.data.msg 37 + message: res.data.msg
37 - }); 38 + });
38 - } 39 + }
39 - }) 40 + })
40 - .catch(err => { 41 + .catch(err => {
41 - console.error(err); 42 + console.error(err);
42 - }); 43 + });
43 -}
44 -</script>
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 } 44 }
70 </script> 45 </script>
71 46
......
...@@ -94,7 +94,8 @@ export default { ...@@ -94,7 +94,8 @@ export default {
94 this.$router.push({ 94 this.$router.push({
95 path: '/client/videoDetail', 95 path: '/client/videoDetail',
96 query: { 96 query: {
97 - prod_id: this.item.id 97 + prod_id: this.item.id,
98 + path: this.item.path // 特殊标识,判断入口 为keepAlive使用
98 } 99 }
99 }); 100 });
100 }, 101 },
...@@ -129,4 +130,4 @@ export default { ...@@ -129,4 +130,4 @@ export default {
129 130
130 } 131 }
131 } 132 }
132 -</style>
...\ No newline at end of file ...\ No newline at end of file
133 +</style>
......
...@@ -98,7 +98,8 @@ export default [{ ...@@ -98,7 +98,8 @@ export default [{
98 name: '个人首页', 98 name: '个人首页',
99 component: () => import('./views/client/personIndex.vue'), 99 component: () => import('./views/client/personIndex.vue'),
100 meta: { 100 meta: {
101 - title: '个人首页' 101 + title: '个人首页',
102 + keepAlive: true
102 }, 103 },
103 children: [] 104 children: []
104 }, { 105 }, {
......
...@@ -114,7 +114,7 @@ import icon_unsubscribe from '@images/icon-dingyue02@2x.png' ...@@ -114,7 +114,7 @@ import icon_unsubscribe from '@images/icon-dingyue02@2x.png'
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, onActivated } from 'vue' 116 import { ref, reactive, onMounted, nextTick, onActivated } from 'vue'
117 -import { useRoute, useRouter } from 'vue-router' 117 +import { useRoute, useRouter, onBeforeRouteLeave } from 'vue-router'
118 import axios from '@/utils/axios'; 118 import axios from '@/utils/axios';
119 import $ from 'jquery' 119 import $ from 'jquery'
120 import _ from 'lodash' 120 import _ from 'lodash'
...@@ -244,9 +244,31 @@ const uploadVideo = () => { ...@@ -244,9 +244,31 @@ const uploadVideo = () => {
244 }) 244 })
245 } 245 }
246 246
247 -onActivated(() => { // keepAlive重置后执行回调 247 +/****************** keepAlive 模块 *******************/
248 - onLoad() 248 +
249 +onActivated(() => { // keepAlive 重置后执行回调
249 }); 250 });
251 +
252 +const changeRouterKeepAlive = (path, keepAlive) => {
253 + $router.options.routes.map((item) => {
254 + if (item.path === path) {
255 + item.meta.keepAlive = keepAlive;
256 + }
257 + });
258 +};
259 +
260 +onBeforeRouteLeave((to, from) => {
261 + // 如果是从页面返回,需要重置keepAlive状态
262 + // 列表页 =》 详情页
263 + // TAG: keepAlive
264 + if (to.path === '/client/videoDetail') {
265 + changeRouterKeepAlive(from.path, true);
266 + } else {
267 + changeRouterKeepAlive(from.path, false);
268 + }
269 +})
270 +
271 +/*********************************************************/
250 </script> 272 </script>
251 273
252 <script> 274 <script>
......
...@@ -55,9 +55,10 @@ ...@@ -55,9 +55,10 @@
55 <script setup> 55 <script setup>
56 import VideoCard from '@/components/VideoCard/index.vue' 56 import VideoCard from '@/components/VideoCard/index.vue'
57 57
58 -import { ref } from 'vue' 58 +import { ref, onActivated } from 'vue'
59 -import { useRoute, useRouter } from 'vue-router' 59 +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 { Toast } from 'vant'; 62 import { Toast } from 'vant';
62 const $route = useRoute(); 63 const $route = useRoute();
63 const $router = useRouter(); 64 const $router = useRouter();
...@@ -71,6 +72,9 @@ axios.get('/srv/?a=perf_info', { ...@@ -71,6 +72,9 @@ axios.get('/srv/?a=perf_info', {
71 }) 72 })
72 .then(res => { 73 .then(res => {
73 if (res.data.code === 1) { 74 if (res.data.code === 1) {
75 + _.each(res.data.data.prod, (item) => {
76 + item.path = 'personIndex'
77 + })
74 userInfo.value = res.data.data; 78 userInfo.value = res.data.data;
75 } else { 79 } else {
76 console.warn(res); 80 console.warn(res);
...@@ -111,6 +115,31 @@ const followUser = (status) => { ...@@ -111,6 +115,31 @@ const followUser = (status) => {
111 }) 115 })
112 }; 116 };
113 117
118 +/****************** keepAlive 模块 *******************/
119 +
120 +onActivated(() => { // keepAlive 重置后执行回调
121 +});
122 +
123 +const changeRouterKeepAlive = (path, keepAlive) => {
124 + $router.options.routes.map((item) => {
125 + if (item.path === path) {
126 + item.meta.keepAlive = keepAlive;
127 + }
128 + });
129 +};
130 +
131 +onBeforeRouteLeave((to, from) => {
132 + // 如果是从页面返回,需要重置keepAlive状态
133 + // 列表页 =》 详情页
134 + // TAG: keepAlive
135 + if (to.path === '/client/videoDetail' && to.query.path) {
136 + changeRouterKeepAlive(from.path, true);
137 + } else {
138 + changeRouterKeepAlive(from.path, false);
139 + }
140 +})
141 +
142 +/*********************************************************/
114 </script> 143 </script>
115 144
116 <script> 145 <script>
...@@ -200,4 +229,4 @@ export default { ...@@ -200,4 +229,4 @@ export default {
200 } 229 }
201 } 230 }
202 } 231 }
203 -</style>
...\ No newline at end of file ...\ No newline at end of file
232 +</style>
......
...@@ -55,7 +55,7 @@ import CommentBox from '@/components/CommentBox/index.vue' ...@@ -55,7 +55,7 @@ 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, onActivated } from 'vue' 57 import { ref, reactive, onActivated } from 'vue'
58 -import { useRoute, useRouter } from 'vue-router' 58 +import { useRoute, useRouter, onBeforeRouteLeave } from 'vue-router'
59 import axios from '@/utils/axios'; 59 import axios from '@/utils/axios';
60 import _ from 'lodash' 60 import _ from 'lodash'
61 import { Toast } from 'vant'; 61 import { Toast } from 'vant';
...@@ -108,8 +108,8 @@ const onLoad = () => { ...@@ -108,8 +108,8 @@ const onLoad = () => {
108 console.error(err); 108 console.error(err);
109 }) 109 })
110 } 110 }
111 - 111 +// 立即执行一次
112 -onLoad() 112 +onLoad();
113 113
114 const showNotice = ref(false) 114 const showNotice = ref(false)
115 const onClose = () => { // 关闭提示框回调 115 const onClose = () => { // 关闭提示框回调
...@@ -201,9 +201,31 @@ const onClick = (item) => { ...@@ -201,9 +201,31 @@ const onClick = (item) => {
201 }); 201 });
202 } 202 }
203 203
204 -onActivated(() => { // keepAlive重置后执行回调 204 +/****************** keepAlive 模块 *******************/
205 - onLoad() 205 +
206 +onActivated(() => { // keepAlive 重置后执行回调
206 }); 207 });
208 +
209 +const changeRouterKeepAlive = (path, keepAlive) => {
210 + $router.options.routes.map((item) => {
211 + if (item.path === path) {
212 + item.meta.keepAlive = keepAlive;
213 + }
214 + });
215 +};
216 +
217 +onBeforeRouteLeave((to, from) => {
218 + // 如果是从页面返回,需要重置keepAlive状态
219 + // 列表页 =》 详情页
220 + // TAG: keepAlive
221 + if (to.path === '/client/bookDetail') {
222 + changeRouterKeepAlive(from.path, true);
223 + } else {
224 + changeRouterKeepAlive(from.path, false);
225 + }
226 +})
227 +
228 +/*********************************************************/
207 </script> 229 </script>
208 230
209 <script> 231 <script>
......
...@@ -286,12 +286,6 @@ const getUnwatch = () => { ...@@ -286,12 +286,6 @@ 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 - },
295 mixins: [mixin.init], 289 mixins: [mixin.init],
296 data() { 290 data() {
297 return { 291 return {
......
...@@ -46,7 +46,7 @@ ...@@ -46,7 +46,7 @@
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, onActivated } from 'vue' 48 import { ref, reactive, onMounted, onActivated } from 'vue'
49 -import { useRoute, useRouter } from 'vue-router' 49 +import { useRoute, useRouter, onBeforeRouteLeave } from 'vue-router'
50 import axios from '@/utils/axios'; 50 import axios from '@/utils/axios';
51 import _ from 'lodash' 51 import _ from 'lodash'
52 import { Toast, Dialog } from 'vant'; 52 import { Toast, Dialog } from 'vant';
...@@ -137,9 +137,31 @@ const deleteComment = (item) => { // 删除评论 ...@@ -137,9 +137,31 @@ const deleteComment = (item) => { // 删除评论
137 }); 137 });
138 } 138 }
139 139
140 -onActivated(() => { // keepAlive重置后执行回调 140 +/****************** keepAlive 模块 *******************/
141 - onLoad() 141 +
142 +onActivated(() => { // keepAlive 重置后执行回调
142 }); 143 });
144 +
145 +const changeRouterKeepAlive = (path, keepAlive) => {
146 + $router.options.routes.map((item) => {
147 + if (item.path === path) {
148 + item.meta.keepAlive = keepAlive;
149 + }
150 + });
151 +};
152 +
153 +onBeforeRouteLeave((to, from) => {
154 + // 如果是从页面返回,需要重置keepAlive状态
155 + // 列表页 =》 详情页
156 + // TAG: keepAlive
157 + if (to.path === '/client/videoDetail') {
158 + changeRouterKeepAlive(from.path, true);
159 + } else {
160 + changeRouterKeepAlive(from.path, false);
161 + }
162 +})
163 +
164 +/*********************************************************/
143 </script> 165 </script>
144 166
145 <script> 167 <script>
......