2

Есть компонент:

Шаблон:

<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>

Код компонента:

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;
    });
},
});

Отображение дочерних списков происходит только после клика на соседний элемент. Как заставить его выводить данные в момент их изменения?

1 ответ 1

1

Документация

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

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.