M
M
Maxim Vlasov2018-02-04 21:28:06
Vue.js
Maxim Vlasov, 2018-02-04 21:28:06

vuejs. Why is the list not showing?

There is a component that displays a list received from api. When the component is mounted, the list is displayed, but when getting child elements, they are displayed only after the adjacent element is clicked.
Sample:

<li :class="{active: active}" class="list-item">
    <span v-if="model.child" >+</span><span @click="activeSet">{{ model.name }}</span>
    <ul v-if="model.child && model.child.length">
        <item :model="item" :key="item.id" v-for="(item,index) in model.child"></item>
    </ul>
</li>

Component code:
Vue.component('item',{
props: ['model'],
template : '#item-template',
data: function () {
    return {
        active: false,
    }
},
methods: {
    activeSet: function(){
        var self = this;
        window.Event.$emit('deactive');
        this.active = true;
        axios.get('api.php',{
            params : {
                folder : self.model.path
            }
        }).then(function(response){
            if(response.data.folders && response.data.folders.length)
                self.model.child = response.data.folders;
        });
    }
},
mounted: function(){
    var self = this;
    window.Event.$on('deactive',function(){
        self.active = false;
    });
}
} );

App:
var app = new Vue({
el: '#app',
data: {
    treeData : []
},
mounted: function(){
    var self = this;
    axios.get('api.php',{
        params : {
            folder : '/'
        }
    }).then(function(response){
        self.treeData = response.data.folders;
    });
},
});

How to force vue to render child element while list is changing?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2018-02-04
@tr0yka

Probably, child is initially absent in model, right? Try
replacing with

self.$set(self.model, 'child', response.data.folders);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question