惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

阮一峰的网络日志
阮一峰的网络日志
D
Darknet – Hacking Tools, Hacker News & Cyber Security
S
Schneier on Security
The Last Watchdog
The Last Watchdog
Cyberwarzone
Cyberwarzone
S
Securelist
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
Cyber Attacks, Cyber Crime and Cyber Security
L
Lohrmann on Cybersecurity
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 司徒正美
The Cloudflare Blog
V
V2EX
博客园_首页
博客园 - 聂微东
Vercel News
Vercel News
人人都是产品经理
人人都是产品经理
G
GRAHAM CLULEY
T
Tenable Blog
Last Week in AI
Last Week in AI
Y
Y Combinator Blog
L
LINUX DO - 最新话题
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
SecWiki News
SecWiki News
博客园 - 三生石上(FineUI控件)
S
Secure Thoughts
N
News | PayPal Newsroom
T
The Blog of Author Tim Ferriss
The GitHub Blog
The GitHub Blog
T
Troy Hunt's Blog
博客园 - 【当耐特】
Forbes - Security
Forbes - Security
H
Hacker News: Front Page
A
About on SuperTechFans
B
Blog RSS Feed
Engineering at Meta
Engineering at Meta
MongoDB | Blog
MongoDB | Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
罗磊的独立博客
D
DataBreaches.Net
P
Privacy & Cybersecurity Law Blog
Schneier on Security
Schneier on Security
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Google DeepMind News
Google DeepMind News
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Jina AI
Jina AI
D
Docker
P
Proofpoint News Feed

卖坚果的怪叔叔

上线8年,15万用户的小程序,一共赚了多少钱? 糟糕的三月 还未见面的小狗,去天堂了。 快过年了!你们放假通知下来了吗? 免费领取一年Gemini Pro使用权益!! 2025年,如梦一场! 西安! WordPress评论留言通知推送插件! 初冬!暖日!东太湖慢行! H5在safari上fixed不触底bug? 2025年国庆的一些记录! 秋末的晚上 我用Trae做了一个一键同步热点要闻到公众号的工具 使用uview-plus遇到的一些问题! Nuxt3中的水合是什么?以及使用中的一些总结! Element Plus Upload 添加支持拖拽排序~ 夏日的周末! 腾讯视频VIP会员免费领取!3-31天。 七月已经到中旬了呀 周末了,但也过得太快了吧。 吐血收集的1000+九号电动车提示音,免费领取! 生活中的突发事件 小毛驴历险记2 小毛驴历险记 QQ,你有多久没有打开啦! npm install 出现 Error:EISDIR:illegal operation on a directory 的错误提示! 最近为啥没更新? 是过年呀 PC微信多开和防撤回~ 好险!我差点被诈骗了~
Nuxt3中piana持久化处理!
坚果大叔 2025-06-22 18:45:45技术阅读 2,881 · 2025-06-22 · via 卖坚果的怪叔叔

在使用nuxt3开发SSR渲染的项目的时候,如何将piana持久化到本地?如何自动引入自定义的仓库store模块,减少手动引入的麻烦?

使用插件 @pinia-plugin-persistedstate/nuxt 来达到持久化的目的。

npm i @pinia-plugin-persistedstate/nuxt 前提也需要安装 nuxt piana模块@pinia/nuxt 。

安装完成后在nuxt.config.js中进行以下配置。

1、在modules中配置刚刚安装的两个模块依赖。

首先设置piniaPersistedstate,cookieOptions是设置过期时间,storage用来指定使用localStorage还是sessionStorage。需要注意⚠️,在服务端渲染的时候没有此关键字。所以需要添加判断只能在客户端的时候持久化。

2、piana 中设置storesDirs,就能直接在页面使用相关store的配置,而无需手动引入。

export default defineNuxtConfig({
  ssr: true,
  compatibilityDate: '2024-11-01',
  devtools: { enabled: true },
  modules: [
    '@pinia/nuxt',
    '@pinia-plugin-persistedstate/nuxt',
  ],
  pinia: {
    storesDirs: ['./store/**']
  },
  piniaPersistedstate: {
    cookieOptions: {
      maxAge: 2 * 365 * 24 * 60 * 60 * 1000
    },
    storage: typeof window !== 'undefined' ? 'localStorage' : 'cookies',
    debug: import.meta.env.DEV
  },
})

另外有的解决方法中,让在plugins 新建如下插件配置,经过测试后其实是没有用的。

import piniaPluginPersistedstate from 'pinia-plugin-persistedstate'
export default defineNuxtPlugin((nuxtApp) => {
    nuxtApp.$pinia.use(piniaPluginPersistedstate)
})

然后在自定义piana的模块中添加 persist:true选项来进行持久化配置。

import { defineStore } from "pinia";
const useBaseData= defineStore("abc", {
    state: () => ({
        data: null as string | null,
    }),
    actions: {
        async updateData(newVer: string) {

        },
    },
    persist: true,
});
export default useBaseData;