Code Monkey home page Code Monkey logo

Comments (2)

Seb-L avatar Seb-L commented on August 17, 2024 3

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.

Borderliner avatar Borderliner commented on August 17, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.