学无先后,达者为师

网站首页 Vue 正文

vue页面加载默认进入vue-router嵌套子路由,并使用keep-alive对组件进行缓存

作者:每一天,每一步 更新时间: 2022-04-22 Vue

需求:

Home组件下有2个子组件:News、Products。

1. 页面加载直接进入home/news;

2. 缓存home组件中的状态。

实现:

Home.vue

    data() {
      return {
        path: '/home/news'
      }
    },    
    // 进入home则激活
    activated() {
      console.log('actived');
      this.$router.push(this.path);
    },
    // 离开home之前将当前路径/home/news赋值给变量path
    beforeRouteLeave(to, from, next) {
      console.log(this.$route)   //  path: '/home/news'
      this.path = this.$route.path;
      next();
    }

App.vue

    
      
    

效果:

 

原文链接:https://blog.csdn.net/u010234868/article/details/121654474

栏目分类
最近更新