'How to import a large array in Vue

I have a single file component with some data:

data () {
    return {
        myArray: [obj1,obj2,...obj10000]
    }
}

Is it a good idea to have 10000 objects in the data array? If not, how can I import the large array in the single file component?



Solution 1:[1]

You can create it in a separate file and import to component.

const animals = [ //animals.js
  {name:"tiger", weight:120},
  {name:"elephant", weight:1000}
]

export default animals

In component

import animals from './path/to/animals.js'

export default {
  data() {
    return {
      animals: animals
    }
  },

Solution 2:[2]

I found that use a separate json file is a good idea.

Solution 3:[3]

Simply create a seperate file in your folder structure under /src /components or wherever.

You shall have two files

  1. data.js / data.json

  2. ComponentName.vue

data.js file, could also be a data.json file.

 export const data  =  [{},{},{}]
 export default data;

To import use in any vue component ----

<template></template>
<script>
import data from './components/data' 
*// in case folder structure is different use   " ../ or ./ "*
</script>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 artoju
Solution 2 JackJack
Solution 3 Pierce D'souza