Vue project, there is an array is obtained by the axios background, multiple components need to be used, how should this array be stored?

the best solution is to use vuex? Why can"t the values in store be assigned directly in data, but must be in computed?

I need each component to have a computed to receive the array in these store.

can anyone give me an example? thank you

Mar.02,2021

  1. vuex is created to solve the problem of using common data between components;
  2. is put in computed to respond to changes, and you can also put it in data;
    but it is not officially supported:

this actually depends on the scene.

if you retrieve the array in the background, it will not change, which is equivalent to a static constant.
for example: ALL_ROLES = ['student',' teacher'] in this way, you can store it in localStorage, pick it up when you need it, and fetch it on the server if you don't have it before you go, which is also very ok. The only drawback may be that the ilocalStorage O of Synchronize is a little slower.

if your array changes from time to time, and several arrays rely on a "live" singleton data, it's best to put it in store. It is also very simple to write. Components that depend on this array dispatch the action, of fetch data every time they created, and then mapGetters is mapped to the component computed. Pseudocode:

// ExComponent.vue
import {mapGetters} from 'vuex'
created () {
    this.$store.dispatch('fetchArrData')
},
computed: {...mapGetters(['arr'])}

it's so simple and cool that you can't find a more elegant way


you can't just save it in localStorage?


vuex learn about


use vuet's mapRules to solve this problem perfectly, and the author developed this for car racing.

<template>
<div class="txt-left txt-b-info">:{{menuEdit.form.name}}</div>
</template>
  

isn't that what vuex is for?

Menu