How to access JSON objects in the vue.js app I am new in this
import json from './json/data.json'
the JSON file is loaded and now I have to access the objects within it
How to access JSON objects in the vue.js app I am new in this
import json from './json/data.json'
the JSON file is loaded and now I have to access the objects within it
Just assign the import to a data property
then loop through the
myJson
property in your template usingv-for
NOTE
If the object you want to import is static i.e does not change then assigning it to a data property would make no sense as it does not need to be reactive.
Vue converts all the properties in the
data
option to getters/setters for the properties to be reactive. So it would be unnecessary and overhead for vue to setup getters/setters for data which is not going to change. See Reactivity in depth.So you can create a custom option as follows:
then loop through the custom option in your template using
$options
:Typescript projects (I have typescript in SFC vue components), need to set
resolveJsonModule
compiler option totrue
.In tsconfig.json:
Happy coding :)
(Source https://www.typescriptlang.org/docs/handbook/compiler-options.html)
If your file looks like this:
You can do: