vue路由在keep-alive下的刷新问题 对watch的影响

转载自:https://www.cnblogs.com/dansingal/p/8302100.html

 

问题描述:

  在keep-alive中的在跳转到指定的路由时刷新对应的路由,其余不刷新。

1
2
3
4
5
<transition name= "fade"  mode= "out-in" >
    <keep-alive>
        <router-view></router-view>
    </keep-alive>
</transition>

有几种解决方式:

  1.在keep-alive中直接添加 include,cachedViews(Array类型:包含vue文件的组件name都将被缓存起来);反之exclude则是不包含;

  注意:所有.vue组件文件都必须附上name属性!!!建议用vuex管理cachedViews

1
2
3
<keep-alive :include= "cachedViews" >
       <router-view></router-view>
</keep-alive>

  2.监测$router的变化;

1
2
3
4
watch: {
     // 如果路由有变化,会再次执行该方法
    "$route" "fetchDate"
}

  但是会在页面离开时再次执行fetchDate,并不是我们需要的,所以可以在to和from上添加执行逻辑,但也是十分的麻烦

1
2
3
4
5
6
7
8
//$router是只读状态,所以赋值操作会失效
watch: {
     $route (to, from) {
         if (list.indexOf(from.path) > -1){  //自行添加逻辑,list为你不想有缓存的路径
         this .getData()
         }  
  }
}

  3.在添加keep-alive后会增加两个生命周期mounted>activated、离开时执行deactivated,路由的进入和切换回相应的触发activated和deactivated,这样就可以在每次入路由执行更细致的操作了

1
2
3
4
5
//如果是服务端渲染就算了
activated() {
     //只刷新数据,不改变整体的缓存
     this .fetchDate();
  }

  4.还有更简单粗暴的

1
2
3
4
5
6
//我就笑笑不说话<br><div>
     <keep-alive>
         <router-view v- if = "$route.meta.keepAlive" ></router-view>
     </keep-alive>
     <router-view v- if = "!$route.meta.keepAlive" ></router-view>
</div>

  5.还有种情况,在不同路由应用了相同的vue组件

1
2
{path: 'aaa' ,component:Mompage,name: 'mom' },
{path: 'bbb' ,component:Mompage,name: 'momPlus' }

  默认情况下当这两个页面切换时并不会触发vue的created或者mounted钩子,需要手动的watch:$router(又回到上面的步骤),或者在router-view上加上唯一值。

1
2
3
4
5
6
7
//随便抄一段代码过来
<router-view :key= "key" ></router-view>
computed: {
     key() {
         return  this .$route.name !== undefined?  this .$route.name + + new  Date():  this .$route + + new  Date()
     }
  }

转载于:https://www.cnblogs.com/blitheG/p/11014871.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 3 中使用 TypeScript 和由结合动态 keep-alive 缓存,你可以使用 Vue Router 和 `<keep-alive>` 组件来实现。 首先,确保你已经安装了 Vue Router 和 Vue 3 的相关依赖。然后,在由配置中添加 `meta` 字段来指定需要缓存的组件。 ```typescript // router.ts import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'; const routes: Array<RouteRecordRaw> = [ { path: '/', name: 'Home', component: () => import('@/views/Home.vue'), meta: { keepAlive: true // 将需要缓存的组件设置为 true } }, // 其他由配置... ]; const router = createRouter({ history: createWebHistory(), routes }); export default router; ``` 接下来,在根组件中使用 `<router-view>` 和 `<keep-alive>` 组件来实现动态缓存。 ```vue <template> <div> <router-view v-slot="{ Component }"> <keep-alive> <component :is="Component"></component> </keep-alive> </router-view> </div> </template> <script> import { onMounted, watch } from 'vue'; import { useRouter } from 'vue-router'; export default { setup() { const router = useRouter(); // 监听由变化,判断是否需要缓存组件 watch( () => router.currentRoute.value, (to, from) => { if (to.meta.keepAlive && from.meta.keepAlive) { // 如果前后两个由都需要缓存,则手动触发组件的生命周期钩子 const component = router.currentRoute.value.matched[0].components.default; component.__vccOpts && component.__vccOpts.activated && component.__vccOpts.activated(); } } ); // 在初次加载时手动触发根组件的生命周期钩子 onMounted(() => { const component = router.currentRoute.value.matched[0].components.default; component.__vccOpts && component.__vccOpts.activated && component.__vccOpts.activated(); }); } }; </script> ``` 在上面的示例中,我们在由配置中将需要缓存的组件的 `meta.keepAlive` 设置为 `true`。然后,在根组件中使用 `<router-view>` 来渲染当前由匹配的组件,并将其包裹在 `<keep-alive>` 组件中。 通过监听由变化,我们可以判断前后两个由是否都需要缓存。如果是,则手动触发组件的 `activated` 生命周期钩子,以确保组件被正确缓存和激活。 请注意,这里我们使用了 Vue 3 的 Composition API 来编写逻辑。确保你的项目已经配置了相关依赖并支持 Composition API。 希望这个示例对你有帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值