V
V
Vlad Avtomat2017-08-28 12:20:18
JavaScript
Vlad Avtomat, 2017-08-28 12:20:18

How to iterate in VUE.JS using v-for objects that I receive from the server?

First I get my objects

created() {
            this.$http.get('/api/main-page/transports/get-transports').then(response => {
                this.personalTransportTabs = response.body.personalTransportTabs;
                this.dataLoaded = true;
            }).catch(error => {
                console.log(error);
            });
        }

After that, as I understand it, you need to transfer this to data
data() {
            return {
                personalTabs: this.personalTransportTabs
            };  
        },

After that, we go through it with the help of v-for.
<el-tab-pane v-for="personalTab in personalTabs" :label="personalTab.name" >{{ personalTab.name }}</el-tab-pane>

What am I doing wrong ?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Shashenkov, 2017-08-28
@VladSavelev

this.$set

created() {
            this.$http.get('/api/main-page/transports/get-transports').then(response => {//Эта функция будет выполнена после инициализации
                this.$set(this,'personalTabs',  response.body.personalTransportTabs);
                this.dataLoaded = true;
            }).catch(error => {
                console.log(error);
            });
        },
data() {
            return {
                personalTabs: []
            };  
        },

E
Evgeny Kulakov, 2017-08-28
@kulakoff Vue.js

The fields in data are reactive, just do an assignment when you get the data:
This will trigger an automatic re-render.
This will not work for you : Because data is initialized once when an instance is created. personalTabs: this.personalTransportTabs

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question