javascript - How to add data obtained by ajax to the component
大家讲道理
大家讲道理 2017-05-19 10:38:29
0
4
538

Is there no way to directly add v-for to a custom component? Why is this part no longer loaded after I add v-for? How can I put the data obtained by ajax here? , please give me some help and advice, I’m very grateful

Vue.component('my-article',{
props:['detail','user'],
template:'<h6>{{detail.title}}</h6>'+
         '<table id="phone">'+
            '<thead>'+
                '<tr>'+
                    '<td style="width: 150px;">{{detail.department}}</td>'+
                    '<td style="width: 119px;">{{detail.phone1}}</td>'+
                    '<td style="width: 121px;">{{detail.phone2}}</td>'+
                '</tr>'+
            '</thead>'+
            '<tbody>'+
                '<tr v-for="data in users" :user="data">'+
                    '<td>{{user.name}}</td>'+
                    '<td>{{user.details1}}</td>'+
                    '<td>{{user.details2}}</td>'+
                '</tr>'+
            '</tbody>'+
        '</table>'
});

So that it can only execute the tr section in a loop

let vm = new Vue({
el:"#module-duty",
data:{
    article:{
        title:"值班单位电话",
        department:"单位",
        phone1:"内线电话",
        phone2:"外线电话"
    },
    users:""
},
created:function(){
    $.ajax({
        url:"http://localhost:8080/opseyetem/post/find.do",
        type:"post",
        dataType:"json",
        success:function(result){
            if(result.state==0){
                vm.users = result.data;
            }
        },
        error:function(){
            alert("请求失败");
        }
    });
}

});

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(4)
为情所困
Vue.component('my-article',{
props:['detail','user'],
template:'<h6>{{detail.title}}</h6>'+
         '<table id="phone">'+
            '<thead>'+
                '<tr>'+
                    '<td style="width: 150px;">{{detail.department}}</td>'+
                    '<td style="width: 119px;">{{detail.phone1}}</td>'+
                    '<td style="width: 121px;">{{detail.phone2}}</td>'+
                '</tr>'+
            '</thead>'+
            '<tbody>'+
                '<tr v-for="data in user">'+
                    '<td>{{data.name}}</td>'+
                    '<td>{{data.details1}}</td>'+
                    '<td>{{data.details2}}</td>'+
                '</tr>'+
            '</tbody>'+
        '</table>'
});

HTML
<my-article :detail="article" :user="users"></my-article>
黄舟

Your props receive details, and you use users. Where did your user come from?

伊谢尔伦

Add a props to the component you defined, such as the cdata attribute, to pass data, and reference the component in the parent component
<childComponent cdata="yourAjaxDATA"><childComponent>, so that the child component can cdata.attr Access the data of your AJAX request

黄舟

The component defined above directly receives a variable detail. Users is a variable of the parent component and cannot be called directly by the child component.
I feel like your writing is a bit messy. Can one ID bind two vue?

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template