hookehuyr

✨ feat(keepAlive模块): 无刷新搭配pinia使用

...@@ -50,7 +50,7 @@ export default { ...@@ -50,7 +50,7 @@ export default {
50 console.error(err); 50 console.error(err);
51 }); 51 });
52 }, 52 },
53 - getProductDetail (type, id) { 53 + getProductDetail (type, id) { // 查询更新作品详情
54 axios.get('/srv/?a=prod_info', { 54 axios.get('/srv/?a=prod_info', {
55 params: { 55 params: {
56 prod_id: id 56 prod_id: id
...@@ -60,6 +60,8 @@ export default { ...@@ -60,6 +60,8 @@ export default {
60 if (res.data.code === 1) { 60 if (res.data.code === 1) {
61 this.detail[`is_${type}`] = res.data.data[`is_${type}`]; 61 this.detail[`is_${type}`] = res.data.data[`is_${type}`];
62 this.detail[`${type}_num`] = res.data.data[`${type}_num`]; 62 this.detail[`${type}_num`] = res.data.data[`${type}_num`];
63 + // 传回数据
64 + this.$emit('on-click', this.detail);
63 } else { 65 } else {
64 // tslint:disable-next-line: no-console 66 // tslint:disable-next-line: no-console
65 console.warn(res); 67 console.warn(res);
...@@ -76,4 +78,4 @@ export default { ...@@ -76,4 +78,4 @@ export default {
76 } 78 }
77 } 79 }
78 } 80 }
79 -};
...\ No newline at end of file ...\ No newline at end of file
81 +};
......
...@@ -6,7 +6,8 @@ export const mainStore = defineStore('main', { ...@@ -6,7 +6,8 @@ export const mainStore = defineStore('main', {
6 msg: 'Hello world', 6 msg: 'Hello world',
7 count: 0, 7 count: 0,
8 auth: false, 8 auth: false,
9 - comment_num: 0 9 + comment_num: 0,
10 + video_detail: {}
10 }; 11 };
11 }, 12 },
12 getters: {}, 13 getters: {},
...@@ -16,6 +17,9 @@ export const mainStore = defineStore('main', { ...@@ -16,6 +17,9 @@ export const mainStore = defineStore('main', {
16 }, 17 },
17 changeCommentNum (num) { 18 changeCommentNum (num) {
18 this.comment_num = num; 19 this.comment_num = num;
20 + },
21 + changeVideoDetail (v) {
22 + this.video_detail = v;
19 } 23 }
20 }, 24 },
21 -});
...\ No newline at end of file ...\ No newline at end of file
25 +});
......
...@@ -97,6 +97,9 @@ ...@@ -97,6 +97,9 @@
97 </template> 97 </template>
98 98
99 <script setup> 99 <script setup>
100 +import { mainStore } from '@/store'
101 +import { storeToRefs } from 'pinia'
102 +
100 import Cookies from 'js-cookie' 103 import Cookies from 'js-cookie'
101 import { useVideoList } from '@/composables/useVideoList.js' 104 import { useVideoList } from '@/composables/useVideoList.js'
102 105
...@@ -113,7 +116,7 @@ import icon_unsubscribe from '@images/icon-dingyue02@2x.png' ...@@ -113,7 +116,7 @@ import icon_unsubscribe from '@images/icon-dingyue02@2x.png'
113 116
114 import ShortcutFixed from '@/components/ShortcutFixed/index.vue' 117 import ShortcutFixed from '@/components/ShortcutFixed/index.vue'
115 import tools from '@/common/tool' 118 import tools from '@/common/tool'
116 -import { ref, reactive, onMounted, nextTick, onActivated } from 'vue' 119 +import { ref, reactive, onMounted, nextTick, onActivated, triggerRef } from 'vue'
117 import { useRoute, useRouter, onBeforeRouteLeave } from 'vue-router' 120 import { useRoute, useRouter, onBeforeRouteLeave } from 'vue-router'
118 import axios from '@/utils/axios'; 121 import axios from '@/utils/axios';
119 import $ from 'jquery' 122 import $ from 'jquery'
...@@ -247,6 +250,21 @@ const uploadVideo = () => { ...@@ -247,6 +250,21 @@ const uploadVideo = () => {
247 /****************** keepAlive 模块 *******************/ 250 /****************** keepAlive 模块 *******************/
248 251
249 onActivated(() => { // keepAlive 重置后执行回调 252 onActivated(() => { // keepAlive 重置后执行回调
253 + // TAG: pinia应用,动态刷新数据
254 + // 处理数据未刷新数据显示问题
255 + const store = mainStore()
256 + // Pinia 解构方法:storeToRefs
257 + const { video_detail } = storeToRefs(store);
258 + // 如果作品信息有变化及时修正
259 + const arr = ref([]);
260 + _.each(prod_list.value, (item) => {
261 + if (item.id === video_detail.value.id) {
262 + item = video_detail.value
263 + }
264 + arr.value.push(item);
265 + })
266 + // 触发更新
267 + prod_list.value = arr.value;
250 }); 268 });
251 269
252 const changeRouterKeepAlive = (path, keepAlive) => { 270 const changeRouterKeepAlive = (path, keepAlive) => {
......
...@@ -53,6 +53,9 @@ ...@@ -53,6 +53,9 @@
53 </template> 53 </template>
54 54
55 <script setup> 55 <script setup>
56 +import { mainStore } from '@/store'
57 +import { storeToRefs } from 'pinia'
58 +
56 import VideoCard from '@/components/VideoCard/index.vue' 59 import VideoCard from '@/components/VideoCard/index.vue'
57 60
58 import { ref, onActivated } from 'vue' 61 import { ref, onActivated } from 'vue'
...@@ -118,6 +121,21 @@ const followUser = (status) => { ...@@ -118,6 +121,21 @@ const followUser = (status) => {
118 /****************** keepAlive 模块 *******************/ 121 /****************** keepAlive 模块 *******************/
119 122
120 onActivated(() => { // keepAlive 重置后执行回调 123 onActivated(() => { // keepAlive 重置后执行回调
124 + // TAG: pinia应用,动态刷新数据
125 + // 处理数据未刷新数据显示问题
126 + const store = mainStore()
127 + // Pinia 解构方法:storeToRefs
128 + const { video_detail } = storeToRefs(store);
129 + // 如果作品信息有变化及时修正
130 + const arr = ref([]);
131 + _.each(userInfo.value.prod, (item) => {
132 + if (item.id === video_detail.value.id) {
133 + item = video_detail.value
134 + }
135 + arr.value.push(item);
136 + })
137 + // 触发更新
138 + userInfo.value.prod = arr.value;
121 }); 139 });
122 140
123 const changeRouterKeepAlive = (path, keepAlive) => { 141 const changeRouterKeepAlive = (path, keepAlive) => {
......
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
13 </div> 13 </div>
14 14
15 <div class="video-player"> 15 <div class="video-player">
16 - <video-detail :item="videoInfo"></video-detail> 16 + <video-detail :item="videoInfo" @on-click="onVideoDetail"></video-detail>
17 </div> 17 </div>
18 18
19 <div class="video-main"> 19 <div class="video-main">
...@@ -42,7 +42,7 @@ import { storeToRefs } from 'pinia' ...@@ -42,7 +42,7 @@ import { storeToRefs } from 'pinia'
42 42
43 import VideoDetail from '@/components/VideoDetail/index.vue' 43 import VideoDetail from '@/components/VideoDetail/index.vue'
44 import { ref, reactive, onMounted, watch } from 'vue' 44 import { ref, reactive, onMounted, watch } from 'vue'
45 -import { useRoute, useRouter } from 'vue-router' 45 +import { useRoute, useRouter, onBeforeRouteLeave } from 'vue-router'
46 import axios from '@/utils/axios'; 46 import axios from '@/utils/axios';
47 import $ from 'jquery' 47 import $ from 'jquery'
48 import { Toast } from 'vant'; 48 import { Toast } from 'vant';
...@@ -112,6 +112,11 @@ onMounted(() => { ...@@ -112,6 +112,11 @@ onMounted(() => {
112 console.error(err); 112 console.error(err);
113 }) 113 })
114 }) 114 })
115 +
116 +const onVideoDetail = (v) => {
117 + // 缓存作品信息,给其他页使用
118 + store.changeVideoDetail(v);
119 +}
115 </script> 120 </script>
116 121
117 <script> 122 <script>
......