简体   繁体   中英

My template for component in Vue.js not working?

My data not while using v-for

this code

const roomsComponent = Vue.extend({
data: function () {
    return {
        rooms : [
            {id : "001", name : "PHP", description : "Entusiatas PHP"},
            {id : "002", name : "Java", description : "Entusiatas Java"},
            {id : "003", name : "C#", description : "Entusiatas C#"},
            {id : "004", name : "C++", description : "Entusiatas C++"},
            {id : "005", name : "Javascript", description : "Entusiatas Javascript"},
            {id : "006", name : "Vue.js", description : "Entusiastas Vue.js"}
        ]
    };
},
template: `
    <div class="col-md-4" v-for="o in rooms">
        <div class="card">
            <div class="card-header">
                {{ o.name }}
            </div>
            <div class="card-body">
                {{ o.description }}
            </div>
        </div>
    </div>`
});
var componentTest = Vue.extend({
template : "<h1>Test</h1>"
});
var router = new VueRouter({
routes: [
    {path: '/chat', component: componentTest},
    {path: '/room', component: roomsComponent}
]
});

new Vue({
el: '#app',
router: router,
template: '<router-view></router-view>'
});

Wrap the code in your template with another <div> . There should be only 1 element inside <template> tag, and since you're using a loop, multiple elements will be rendered

<div class="row">
  <div class="col-md-4" v-for="o in rooms">
     ...
  </div>
</div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM