Comments (2)
I did not try the plugin with the "function" method of defining a store, but based on what I see in the sources of Pinia, the third argument of defineStore
is the store options.
/**
* Creates a `useStore` function that retrieves the store instance
*
* @param id - id of the store (must be unique)
* @param storeSetup - function that defines the store
* @param options - extra options
*/
export declare function defineStore<Id extends string, SS>(id: Id, storeSetup: () => SS, options?: DefineSetupStoreOptions<Id, _ExtractStateFromSetupStore<SS>, _ExtractGettersFromSetupStore<SS>, _ExtractActionsFromSetupStore<SS>>): StoreDefinition<Id, _ExtractStateFromSetupStore<SS>, _ExtractGettersFromSetupStore<SS>, _ExtractActionsFromSetupStore<SS>>;
You might be able to use it like so:
defineStore('myStore', () => {
return {}
}, {
persist: {
enabled: true,
...
},
})
I'll try it out, and I'll add it to the documentation.
from pinia-plugin-persist.
Same. My store is defined like this:
export const useUserStore = defineStore('user', () => {
const name = ref('')
const email = ref('')
const role = ref('user')
const apiKey = ref('')
const getName = computed(() => name)
const getemail = computed(() => email)
const getRole = computed(() => role)
const getApiKey = computed(() => apiKey)
const isLoggedIn = computed(() => !!apiKey)
function setName(userName: string) {
name.value = userName
}
function setEmail(userEmail: string) {
email.value = userEmail
}
function setApiKey(userApiKey: string) {
apiKey.value = userApiKey
}
const cookiesStorage: Storage = {
setItem(key, state) {
return Cookies.set('accessToken', apiKey.value, { expires: 7 })
},
getItem(key) {
return JSON.stringify({
accessToken: Cookies.getJSON('accessToken'),
})
},
}
return {
setName,
setEmail,
setApiKey,
getName,
getemail,
getRole,
getApiKey,
isLoggedIn,
}
})
How do I use this persist plugin?
from pinia-plugin-persist.
Related Issues (20)
- The custom storage is inited during define instead of use, which is imcompitible with hooks. HOT 1
- sessionStorage is not defined HOT 2
- A weird bug that loses data when I refresh the page HOT 13
- How to persist state if the default state has not changed?
- Documentation for Nuxt 3 setup HOT 2
- need package.json flag to allow importing to ESM (.mjs) modules
- 在pinia中默认组件卸载后订阅的更新函数会被删除
- 当组件被卸载时subscriptions将被自动删除 HOT 1
- After the data is persisted in the default way, the data cannot be updated HOT 1
- config to vite-plugin-ssr plugin HOT 2
- Vue 2.7 + pinia + persist-cache Not working HOT 1
- could set a dynamic custom storage key ?
- com
- Proposal to Add Cross-Page State Synchronization to Pinia Plugin Persist
- TypeScript5.0+ moduleResolution bundler breaks types import HOT 2
- Nuxt 2 TS error
- trap returned falsish for property
- Nuxt 3.7.2 runtime error HOT 1
- export { index as default, updateStorage }
- When persisted into an array, the underlined fields will be changed to camel case.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pinia-plugin-persist.