Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

vue中为什么页面被包含<router-view />,keep-alive就会失效

最近在vue的实战中,发现一个问题。子路由的页面中如果存在<router-view />,我在进行页面切换的时候,order.vue页面不会被缓存。当我把order.vue注释掉之后,order.vue中的时间又可以被缓存了。

router.js

const routes = [
  {
    path: '/',
    name: 'Home',
    component: () => import('../views/Home.vue'),
    children: [
      {
        path: 'order',
        name: 'order',
        component: () => import('../views/order.vue'),
        children: [
          {
            path: 'order-time',
            name: 'OrderTime',
            component: () => import('../views/orderTime.vue')
          }
        ]
      },
      {
        path: 'mine',
        name: 'mine',
        component: () => import('../views/mine.vue')
      }
    ]
  }
]

home.vue

  <div class="home">
    <router-link :to="{ name: 'order' }">订单</router-link> |
    <router-link :to="{ name: 'mine' }">我的</router-link>
    <keep-alive>
      <router-view />
    </keep-alive>
  </div>

order.vue

<template>
  <div class="order">
    儿子路由:{{ new Date().getTime() }}
    <button @click="back">退出孙子路由</button>
    <button @click="inRouter">进入孙子路由</button>
    <router-view />
  </div>
</template>

<script>
export default {
  methods: {
    back() {
      this.$router.go(-1);
    },
    inRouter() {
      this.$router.push({
        path: "/order/order-time",
      });
    },
  }
};
</script>

mine.vue

<template>
  <div class="mine">
    我的页面
    {{ new Date().getTime() }}
  </div>
</template>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.7k users

...