I'm using wbepack to compile a VUEJS project in which I import a JSON file which has an array of objects into vueJS however when accessing it via the component the object appears to be empty.
import Jason from './some.json';
export defaults {
data(){ return { someArr: Jason } } }
I'm not getting any compile errors or any other reported errors.
What could cause the object someArr
to be empty?
P.S. I am able to load the json via AJAX successfully
To get data from a local JSON file, we need to import the file into the component where we want to process the data. We do the import just like we would with a component, except the component name is replaced with whatever we want to call the data set.
There are two ways to import a JavaScript library to the Vue Component. The first is to import a local JavaScript library. Here, you can import the JavaScript library by using the 'import' keyword inside the script tag of your Vue file. import * as mykey from '../assets/js/mykey.
JSON is purely a string with a specified data format — it contains only properties, no methods. JSON requires double quotes to be used around strings and property names. Single quotes are not valid other than surrounding the entire JSON string.
A JSON object contains zero, one, or more key-value pairs, also called properties. The object is surrounded by curly braces {} . Every key-value pair is separated by a comma. The order of the key-value pair is irrelevant.
Install json loader with
npm install json-loader --save
then in your webpack.config file add this loader
module: {
loaders: [
{
test: /\.json/,
loader: 'json',
}
],
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With