search

Home  >  Q&A  >  body text

javascript - How to add data obtained by ajax to the component

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("请求失败");
        }
    });
}

});

大家讲道理大家讲道理2752 days ago540

reply all(4)I'll reply

  • 为情所困

    为情所困2017-05-19 10:40:29

    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>

    reply
    0
  • 黄舟

    黄舟2017-05-19 10:40:29

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

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-05-19 10:40:29

    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

    reply
    0
  • 黄舟

    黄舟2017-05-19 10:40:29

    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?

    reply
    0
  • Cancelreply