npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@moluoxixi/keepallalive

v1.3.10

Published

KeepAllAlive 组件

Readme

KeepAllAlive 路由缓存组件

一个基于Vue 3的路由组件缓存方案,提供更灵活的路由组件缓存控制能力,支持基于路由参数的缓存控制。

特性

  • 基于路径的缓存:使用路由的完整路径作为缓存键
  • 条件缓存:支持通过路由元数据、查询参数或自定义函数控制是否缓存组件
  • 缓存清理:提供清除特定路由或所有缓存的方法
  • 与Vue Router集成:可直接替代常规router-view与keep-alive组合

使用方法

基本用法

<template>
  <div>
    <keep-all-alive>
      <!-- 内容会被自动缓存 -->
    </keep-all-alive>
  </div>
</template>

<script setup>
import KeepAllAlive from '@moluoxixi/components/KeepAllAlive'
</script>

通过查询参数控制缓存

默认情况下,组件会检查路由的查询参数中是否包含keepAlive=true来决定是否缓存该路由:

// 这个路由会被缓存
/user/profile?keepAlive=true

// 这个路由不会被缓存
/user/profile?keepAlive=false

通过路由元数据控制缓存

在路由配置中设置meta.keepAlive属性:

// router.js
const routes = [
  {
    path: '/user/profile',
    component: UserProfile,
    meta: {
      keepAlive: true // 此路由会被缓存
    }
  },
  {
    path: '/user/settings',
    component: UserSettings,
    meta: {
      keepAlive: false // 此路由不会被缓存
    }
  }
]

自定义缓存逻辑

通过传递defaultKeepAlive函数来自定义缓存判断逻辑:

<template>
  <keep-all-alive :default-keep-alive="shouldKeepAlive">
    <!-- 内容 -->
  </keep-all-alive>
</template>

<script setup>
import KeepAllAlive from '@moluoxixi/components/KeepAllAlive'

// 自定义函数决定是否缓存
const shouldKeepAlive = (route) => {
  // 例如:只缓存特定模块的路由
  return route.path.startsWith('/dashboard')
}
</script>

手动清理缓存

<template>
  <div>
    <button @click="clearPathCache">清除特定路由缓存</button>
    <button @click="clearAllCache">清除所有缓存</button>
    <keep-all-alive ref="keepAliveRef">
      <!-- 内容 -->
    </keep-all-alive>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import KeepAllAlive from '@moluoxixi/components/KeepAllAlive'

const keepAliveRef = ref(null)

// 清除特定路由缓存
const clearPathCache = () => {
  keepAliveRef.value?.clearCache('/user/profile?id=1')
}

// 清除所有缓存
const clearAllCache = () => {
  keepAliveRef.value?.clearAllCache()
}
</script>

API

Props

| 属性名 | 说明 | 类型 | 默认值 | | --- | --- | --- | --- | | defaultKeepAlive | 自定义缓存判断函数,返回true表示缓存,false表示不缓存 | Function | null |

方法

| 方法名 | 说明 | 参数 | | --- | --- | --- | | clearCache | 清除特定路由的缓存 | fullPath: string (路由的完整路径,包括查询参数) | | clearAllCache | 清除所有缓存 | - |

注意事项

  • 缓存的键是路由的完整路径(fullPath),这意味着不同参数的相同路由会被视为不同的缓存实例
  • 默认情况下,组件会检查路由查询参数中的keepAlive或路由meta中的keepAlive属性来决定是否缓存
  • 如果提供了defaultKeepAlive函数,它将优先被使用来决定是否缓存路由