keep-alive缓存文章列表案例完整代码(Vue2)

keep-alive缓存文章列表案例完整代码(Vue2)

环境准备

先创建一个 vue2 项目

bash 复制代码
vue create 项目名

安装 vue-router,vue2 是 3,vue3 是 4

bash 复制代码
npm insatll vue-router@3

目录结构

完整代码

main.js

这里要注册路由

js 复制代码
import Vue from "vue";
import App from "./App.vue";
import router from "./router";

Vue.config.productionTip = false;

new Vue({
  router,
  render: (h) => h(App),
}).$mount("#app");

App.vue

两种写法:

  1. 可以用include里面写需要缓存的组件名称 name,这样的话就要在 router/index.js 中给组件写name。

    router/index.js

    js 复制代码
    routes: [
        {
          path: "/list",
          component: ArticleList,
          name: "ArticleList",
        },
        // ...

    App.vue

    js 复制代码
    <template>
      <div>
      	// <keep-alive :includes="['ArticleList',...]">
        <keep-alive includes="ArticleList">
          <router-view />
        </keep-alive>
      </div>
    </template>
  2. 在 meta 中写明这个组件是否需要被缓存,然后在 Vue.js 中根据不同的值判断这个组件是否需要被 keep-alive 包裹,即是否被缓存。
    router/index.js

    js 复制代码
    routes: [
        {
          path: "/list",
          component: ArticleList,
          meta: { keepAlive: true },
          name: "ArticleList",
        },
        // ...

    App.vue

    js 复制代码
    <template>
      <div>
        <keep-alive>
          <router-view v-if="$route.meta.keepAlive" />
        </keep-alive>
        <router-view v-if="!$route.meta.keepAlive" />
      </div>
    </template>

ArticleList.vue

js 复制代码
<template>
  <div>
    <div v-for="item in articles" :key="item.id" class="item">
      <router-link :to="{ name: 'ArticleDetail', query: { id: item.id } }">
        {{ item.title }}
      </router-link>
    </div>
  </div>
</template>

<script>
export default {
  name: "ArticleList",
  data() {
    return {
      articles: [],
      scrollTop: 0,
    };
  },
  mounted() {
    // console.log("mounted");
    this.articles = this.fakeFetch();
    window.addEventListener("scroll", this.handleScroll);
  },
  // 进入文章列表页面时
  activated() {
    this.$nextTick(() => {
      // console.log("activated:", this.scrollTop);
      window.scrollTo(0, this.scrollTop);
      window.addEventListener("scroll", this.handleScroll);
    });
  },
  // 离开文章列表页面时
  deactivated() {
    // console.log("deactivated:" + this.scrollTop);
    // 将当前的位置保存下来
    window.removeEventListener("scroll", this.handleScroll);
  },
  methods: {
    fakeFetch() {
      return Array.from({ length: 100 }, (_, i) => ({
        id: i + 1,
        title: "文章标题 " + (i + 1),
      }));
    },
    handleScroll() {
      this.scrollTop = window.scrollY || document.documentElement.scrollTop;
      // console.log("滚动位置:", this.scrollTop);
    },
  },
};
</script>

<style>
.item {
  padding: 16px;
  border-bottom: 1px solid #eee;
}
</style>

ArticleDetail.vue

js 复制代码
<template>
  <div>
    <h2>文章详情 {{ $route.query.id }}</h2>
    <p>内容详情...</p>
    <router-link to="/list">返回列表</router-link>
  </div>
</template>
<script>
export default {
  name: "ArticleDetail",
  mounted() {
    // 模拟请求内容
    console.log("详情页 mounted,ID:", this.$route.query.id);
  },
};
</script>

router.js

js 复制代码
import Vue from "vue";
import Router from "vue-router";
import ArticleList from "@/components/ArticleList.vue";
import ArticleDetail from "@/components/ArticleDetail.vue";

Vue.use(Router);

export default new Router({
  routes: [
    {
      path: "/list",
      component: ArticleList,
      meta: { keepAlive: true },
      name: "ArticleList",
    },
    {
      path: "/detail",
      component: ArticleDetail,
      meta: { keepAlive: false },
      name: "ArticleDetail",
    },
  ],
});

Vue3 版本

keep-alive缓存文章列表案例完整代码(Vue3)

相关推荐
java1234_小锋1 小时前
REDIS集群会有写操作丢失吗?为什么
数据库·redis·缓存
程序定小飞1 小时前
基于springboot的学院班级回忆录的设计与实现
java·vue.js·spring boot·后端·spring
攀小黑1 小时前
基于若依-内容管理动态修改,通过路由字典配置动态管理
java·vue.js·spring boot·前端框架·ruoyi
Rysxt_2 小时前
Vuex 教程 从入门到实践
前端·javascript·vue.js
hzk的学习笔记4 小时前
Redisson 的 Watchdog 机制
数据库·redis·分布式·缓存
一 乐5 小时前
高校教务|教务管理|基于springboot+vue的高校教务管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·教务管理
zhousenshan5 小时前
ES6 import语法
vue.js
码界筑梦坊5 小时前
243-基于Django与VUE的笔记本电脑数据可视化分析系统
vue.js·python·信息可视化·数据分析·django·毕业设计·echarts
baozj5 小时前
🚀 手动改 500 个文件?不存在的!我用 AST 撸了个 Vue 国际化神器
前端·javascript·vue.js
hzk的学习笔记7 小时前
Redisson解锁失败,watchdog会不会一直续期下去?
数据库·redis·缓存