7

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

5 Answers 5

4

Install json loader with

npm install json-loader --save

then in your webpack.config file add this loader

module: {
    loaders: [
        {
            test: /\.json/,
            loader: 'json',
        }
    ],
}
Sign up to request clarification or add additional context in comments.

2 Comments

"Since webpack >= v2.0.0, importing of JSON files will work by default. You might still want to use this if you use a custom file extension. See the v1.0.0 -> v2.0.0 Migration Guide for more information" extract from the Webpack Documentation.
Note, since Webpack 4, loaders is now rules.
3

Go to SCR and find a file named shims-vue.d.ts and add a JSON module like this.

    declare module '*.json' 
{
  const value: { [key: string]: any };
  export default value;
}

Comments

1

probably just a typo but you export default { not defaults

Assuming this isn't an issue are you exporting the JSON within ./some.json? i.e.

export default {
  "foo": "bar",
  ...
}

Comments

1

This is how I do it:

In main.js:

import Conf from './static/app-conf.json';
Vue.prototype.$appConfig = Conf;

Now I can use that JSON-file whereever I need it by adding it to data:

    <template>
      <div>{{someText}}</div>
    </template>
    <script>        
        export default {
                name: 'Something',
                components: {},
                props: {},
                data: () => ({
                    someText: Vue.prototype.$appConfig.someText,
                }),
                computed: {},
                methods: {}
            };
    </script>

Comments

-1

This should work:

var yourJSONData= JSON.parse(Jason);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.