D
D
Dauren S2020-10-04 18:15:53
Vue.js
Dauren S, 2020-10-04 18:15:53

Show first element computed Vue.js?

Why is there an error in the console when trying to show the first element of a list from computed
main.js:87126 [Vue warn]: Error in render: "TypeError: Cannot read property '0' of null"
Although everything is displayed on the page .

<div class="col-sm-9">
              <div class="jumbotron">
                   {{itemsList[0]}}
                    <hr class="my-4">
            </div>
        </div>

computed: {
        itemsList() {
            return this.$store.getters.MY_ITEMS;
        },       
    }


store
export const SET_MY_ITEMS = 'SET_MY_ITEMS';

export const myStore = {
    state: {
        my_items: null,
    },
    getters: {
      MY_ITEMS: state => {
        return state.my_items;
      },
    },
    mutations: { // commit
        [SET_MY_ITEMS](state, my_items) {
            state.my_items = my_items;
        },
       
    },
    actions: { // dispatch
      [SET_MY_ITEMS](context) {
        axios.post('/api/index').then((response) => {
            context.commit('SET_MY_ITEMS', response['data']['list']);
        }).catch(error => {
        });
    },
      
    },
   
};

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex, 2020-10-04
@dauren101

Where does the data in MY_ITEMS come from? If they are loaded from somewhere, then most likely they are not there yet at the time of rendering. And there null

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question