How to access Nuxt context variable from Vuex action
Asked Answered
B

2

6

I want to access context variable to use the isMobile flag to select a different endpoint depending on the result. I could pass it on the dispatch from the component, but I know there should be one way to do it.

export const actions = {
...
  signUpGoogle({ commit }) {
    fireauth.useDeviceLanguage()

    if (context.isMobile) {
      fireauth.signInWithPopup(GoogleProvider).then ...

    } else {
      fireauth.signInWithRedirect(GoogleProvider)
    }

}

I saw here that it can be obtained on server init, but I really don't want to rely on this as caching will mess things up

https://nuxtjs.org/guide/vuex-store/#the-nuxtserverinit-action

Thanks for the help

Buchholz answered 28/4, 2019 at 12:28 Comment(1)
not quite understand what u mean. But nuxtServerInit executes on first request each time and there no cachingSikora
K
0

I'm not sure if you're talking about an environment variable here, but below is the answer if it is.

Looking on this GH issue, we can find out that you can use this.app.$config.isMobile (basically referencing env variables aka publicRuntimeConfig values in your nuxt.config.js).

Keeler answered 3/1, 2021 at 18:33 Comment(0)
R
0

You can do one thing in nuxtServerInit, set this variable in state using the context, and then use state.isMobile to do this type of API Calls. Hopefully, that should solve this. If it's not very clear, I can edit to give some code examples

Rothberg answered 1/8, 2020 at 18:18 Comment(0)
K
0

I'm not sure if you're talking about an environment variable here, but below is the answer if it is.

Looking on this GH issue, we can find out that you can use this.app.$config.isMobile (basically referencing env variables aka publicRuntimeConfig values in your nuxt.config.js).

Keeler answered 3/1, 2021 at 18:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.