简体   繁体   中英

VueJS: How to import JSON data into array

I am having some trouble getting a local json file into an array (It needs to be an array because it needs to be shuffled and other range of methods)

I am using javascript

the json file stored as in./assets/events.json goes like this: (it's dummy data)

[
    {
        "id": "00",
        "avatar": "https://aaaaaaaa.png",
        "name": "name1",
        "text": "text1",
        "option1": "lorem",
        "option2": "ipsum"
    },

    {
        "id": "01",
        "avatar": "https://bbbbbbbbbbbbbbb.png",
        "name": "name2",
        "text": "text2",
        "option1": "loremp1",
        "option2": "ipsum2"
    },
   
]

So the problem is to create an array inside a component and getting that information inside it.

data(){
        return{
            informationEvents:[ /*here should be the data*/]
        }

Thank you.

You can import and use after that.

import jsonFile from './assets/events.json'

And.

computed: {
  list: function () {
    return jsonFile
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM