Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

300
Visualizações
How to use plugin's data in a nuxt.config.js file?

My plugin, env.js:

export default async (_ctx, inject) => {
  const resp = await fetch('/config.json')
  const result = await resp.json()
  inject('env', result)

  // eslint-disable-next-line no-console
  console.log('env injected', result)

  return result
}

Then an idea was to use it's data inside nuxt.config.js to inject into publicRuntimeConfig:

import env from './plugins/env.js'

publicRuntimeConfig: {
  test: env,
},

Then in a browser console i'm checking it:

this.$nuxt.$config

It shows me:

enter image description here

instead of a value, though this.$nuxt.$env shows the correct values:

enter image description here

What's wrong?

UPDATE 1

Tried Tony's suggestion:

// nuxt.config.js
import axios from 'axios'

export default async () => {
  const resp = await axios.get('/config.json')
  const config = resp.data

  return {
    publicRuntimeConfig: {
      config
    }
  }
}

It cannot fetch config.json, but if i point it to an external resource: "https://api.openbrewerydb.org/breweries" it does work.

Intention of this question, is to have config.json where a user could simply change variable values there (from a compiled code) and change endpoints without a re-build process.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

In nuxt.config.js, your env variable is a JavaScript module, where the default export is the function intended to be automatically run by Nuxt in a plugin's context. Importing the plugin script does not automatically execute that function. Even if you manually ran that function, it wouldn't make sense to use an injected prop as a runtime config because the data is already available as an injected prop.

If you just want to expose config.json as a runtime config instead of an injected prop, move the code from the plugin into an async configuration:

// nuxt.config.js
export default async () => {
  const resp = await fetch('/config.json')
  const config = await resp.json()

  return {
    publicRuntimeConfig: {
      keycloak: config
    }
  }
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda