question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is it possible to access Nuxt context from a pinia sotre?

See original GitHub issue

First of all thanks for this awesome library.

When I’m using nuxt to build my apps, I normally tend to use Nuxt axios and auth modules for instance and have my API methods wrapped around vuex accessing this.$axios to perform HTTP requests without being worried about authentication and other configuration stuff.

I want to know if it is possible to have nuxt context injected inside my pinia store so i can keep using this pattern? If so how can I achieve this?

Thanks.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

7reactions
posvacommented, Mar 31, 2021
5reactions
isneezycommented, Jun 18, 2020

Ok, after digging a bit I found that with nuxt-composition-api all you have to do is call useContext and everything works.

Note I’m doing in spa mode IDK if it work as intended on SSR but the docs says it does.

// store/Users.ts
import { createStore } from 'pinia'
import { useContext } from 'nuxt-composition-api'

export type User = {
  id: number
  name: String
  username: String
  email: String
  password: String
}

export const useUsersStore = createStore({
  id: 'users',
  state: () => ({
    users: {} as { [id: number]: User }
  }),
  actions: {
    fetchUsers() {
      const { $axios } = useContext() // 👈️ here
      return $axios.$get<User>('/api/users').then((user) => (this.state.users[user.id] = user))
    }
  }
})
Read more comments on GitHub >

github_iconTop Results From Across the Web

Is it possible to access Nuxt context from a pinia sotre? #186
I want to know if it is possible to have nuxt context injected inside my pinia store so i can keep using this...
Read more >
Nuxt.js - Pinia
Using Pinia with Nuxt.js is easier since Nuxt takes care of a lot of things when it comes to server side rendering. For...
Read more >
Context and Helpers - Nuxt
First and foremost, the context is used to provide access to other parts of the Nuxt application, e.g. the Vuex store or the...
Read more >
Using Modules and Pinia to structure Nuxt 3 app
Is it possible to use Pinia without a component in the module to store data eg: logged in user data and access it...
Read more >
How to use the nuxt context in an vuex store? - Storyblok
Using the current Nuxt context and therefore $storyapi and $storybridge the easiest way to achieve that is to pass the parts of the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found