danielroe/typed-vuex

How to get store data out of vue component such as in plugin?

Closed this issue · 5 comments

I want to use store's state data in this plugin
what should I do ?

When I try to use Vue.prototye.$accessor,it returned an undefined.
And When I try to get Vue.prototye.$accessor in setTimeout,it throw an error: "Uncaught TypeError: Cannot read property '$options' of undefined"

this is a minimal repro.
https://codesandbox.io/s/practical-aryabhata-nw931?file=/plugins/report.ts

I hope I made it clear

Thanks

@JanusSpark Would you be happy to use it in a plugin that accesses the Nuxt context, or do you want to use it just in the body of the file?

@JanusSpark Would you be happy to use it in a plugin that accesses the Nuxt context, or do you want to use it just in the body of the file?

@danielroe think both methods should solve the problem.I just don't know how to get the Nuxt context or how to get the instance of the store in the plugin file...

@JanusSpark

import type { Plugin } from '@nuxt/types'

const myPlugin: Plugin = (ctx) => {
  // You have access to the typed store accessor using ctx.app.$accessor
  ctx.app.$accessor.someProperty
}

export default myPlugin

@P4sca1 thanks! - and apologies @JanusSpark that I missed your reply. Let me know if that doesn't solve the issue for you.

@JanusSpark

import type { Plugin } from '@nuxt/types'

const myPlugin: Plugin = (ctx) => {
  // You have access to the typed store accessor using ctx.app.$accessor
  ctx.app.$accessor.someProperty
}

export default myPlugin

@P4sca1 Thanks!It works.
@danielroe Thank you all the same.You helped me a lot about Nuxt