1. 程式人生 > 其它 >Vue keep-alive 快取元件

Vue keep-alive 快取元件

快取路由元件

keep-alive標籤包括,加上include或exclude屬性即可

屬性值必須和元件的name屬性對應

App.vue

<template>
  <div id="app">
    <div class="nav">
      <router-link
        class="nav"
        :to="`/Island?id=${id}&title=${title}`"
        active-class="active-nav"
      >
        Island
      </router-link>
      <router-link class="nav" replace to="/Polaris" active-class="active-nav">
        Polaris
      </router-link>
    </div>
    <div class="con">
      <keep-alive include="Island">
        <router-view />
      </keep-alive>
    </div>
  </div>
</template>

<script>
export default {
  name: "App",
  data: () => ({ id: 123, title: "my title" }),
};
</script>


Island.vue

<template>
      <h1>
          message by Island 
      {{id}} 
      -
      {{title}}
      </h1>
</template>

<script>
export default {
  props:['id','title'],
  name:'Island',
  beforeDestroy(){
    console.log('Isalnd 即將被銷燬');
  }
};
</script>

<style scoped>
h1 {
  color: salmon;
}
</style>