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

145
Visualizações
Vuex import local JSON file into state

I want to import data from my local JSON file into a state value setting.

store/index.js

import { createStore} from 'vuex'

export default createStore({
  state: {
    settings: {}
  }
})

assets/json/settings.json

{
  "theme": "dark",
  "loggedIn": true,
  "mobile": false,
}

It also has to be reactive to work with computed in Vue. This means it should use maybe something like fs or so to save the changes into the JSON file. I use Vuex because I want to change data across multiple components.

How I can set the JSON value into the state.settings value? 😅

about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

you can import directly from a json file like you would a js file

which you can then return from the state initialiser

import settings from "./settings.json";
export default createStore({
  state(){ //note: state should always be a function to create a state not the state itself
    return {
       settings: settings
    }
  }
})

just be sure you have

"resolveJsonModule": true

in your js compiler options

the browser shouldn't be editing this file directly so save changes in a cookie, local storage,session storage, or via a webservice call to your server

about 4 years ago · Santiago Gelvez 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