Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import JSON into VueJS

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

like image 1000
KArneaud Avatar asked Nov 21 '16 15:11

KArneaud


People also ask

How do I get data from JSON file in Vue?

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.

How do I import JavaScript into Vue?

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.

What is a JSON string?

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.

What is JSON structure?

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.


1 Answers

Install json loader with

npm install json-loader --save

then in your webpack.config file add this loader

module: {
    loaders: [
        {
            test: /\.json/,
            loader: 'json',
        }
    ],
}
like image 154
AldoRomo88 Avatar answered Sep 21 '22 19:09

AldoRomo88