search

Home  >  Q&A  >  body text

javascript - New to Vue, how to use the v-for directive in components

code show as below:

Vue.component('child-i',{ 
props:['msg','datato'],
template:'<h1>{{msg.title}}</h1>'+
        '<table>'+
        '<tbody>'+
            '<tr v-for="to in todata" :datato="to">'+
                '<td>{{datato.tile1}}</td>'+
                '<td>{{datato.tile2}}</td>'+
                '<td>{{datato.tile3}}</td>'+
            '</tr>'+
            '</tbody>'+
        '</thead>'
});

Second code

var datas = new Vue({
el:"#app",
data:{
    parameter:{title:"hello vue js"},
    todata:[{
        tile1:"aaa",
        tile2:"www",
        tile3:"sss",
    },{
        tile1:"aaa",
        tile2:"www",
        tile3:"sss",
    },{
        tile1:"aaa",
        tile2:"www",
        tile3:"sss",
    }]
}
});

HTML

<p id="app" >
    <child-i :msg="parameter"></child-i>
</p>

In fact, what I want to do is very simple. I want to use the v-for instruction to dynamically construct a set of tr and td table tags inside the subcomponent. When I run it, the browser does not report an error, but there is no error in the tag. Any tags about tr and td, did I write them wrong? , I hope the master can give some advice or provide a demo for reference

ringa_leeringa_lee2755 days ago628

reply all(2)I'll reply

  • 阿神

    阿神2017-05-19 10:39:59

    You have already sent the msg and also the todata...

    reply
    0
  • 世界只因有你

    世界只因有你2017-05-19 10:39:59

    HTML

    <p id="app" >
        <child-i :msg="parameter" :datato="todata"></child-i>
    </p>
    

    JS

    Vue.component('child-i',{
    props:['msg','datato'],
    template:'<h1>{{msg.title}}</h1>'+
            '<table>'+
            '<tbody>'+
                '<tr v-for="to in datato">'+
                    '<td>{{to.tile1}}</td>'+
                    '<td>{{to.tile2}}</td>'+
                    '<td>{{to.tile3}}</td>'+
                '</tr>'+
                '</tbody>'+
            '</thead>'
    });

    reply
    0
  • Cancelreply