Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

144
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
ยฉ 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!