Answer the question
In order to leave comments, you need to log in
How to correctly output data from API in Vuex 4?
Tell me what am I doing wrong?
import {createStore} from 'vuex'
const axios = require('axios');
const baseURL = 'https://swapi.dev/api/people/';
export default createStore({
state: {
characters: [],
},
mutations: {
SET_CHARACTERS: (state, payload) => {
state.characters = payload;
},
},
actions: {
GET_CHARACTERS: async (context) => {
let data = await axios.get(baseURL);
context.commit('SET_CHARACTERS', data)
}
},
getters: {
CHARACTERS: state => {
return state.characters;
}
},
modules: {}
})
<template>
<div class="card">
<div>{{ $store.state.characters }}</div>
</div>
</template>
<script>
import {mapGetters} from 'vuex';
export default {
computed: {
...mapGetters(['CHARACTERS'])
}
}
</script>
<style lang="scss" src="./card.scss">
</style>
Answer the question
In order to leave comments, you need to log in
Where is the action call GET_CHARACTERS
? I do not see. We must add.
let data = await axios.get(baseURL);
context.commit('SET_CHARACTERS', data)
const response = await axios.get(baseURL);
context.commit('SET_CHARACTERS', response.data.results);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question