我正在尝试将此JavaScript代码转换为Vue JS ES5。这是显示表的Javascript:
window.addEventListener("load", function(){
// simple array
var data = ["cat", "mouse", "bird", "goat", "monkey", "giraffe","cow","donkey","mice", "camel", "elephant", "bufalo", "jade","zebra","goose","hen","zat"];
// html table
var perrow = 4, // 7 items per row
html = "<table><tr>";
// Loop through array and add table cells
for (var i=0; i<data.length; i++) {
html += "<td>" + data[i] + "</td>";
// Break into next row
var next = i+1;
if (next%perrow==0 && next!=data.length) {
html += "</tr><tr>";
}
}
html += "</tr></table>";
document.getElementById("container").innerHTML = html;
});
我如何转换为vue js。我的VUE代码是
<script type="text/javascript">
var appVM= new Vue({
el:'#app',
mydata:{["cat", "mouse", "bird", "goat", "monkey", "giraffe","cow","donkey","mice", "camel", "elephant", "bufalo", "jade","zebra","goose","hen","zat"];
},
</script>
<table id="myTable" class="display table" width="100%">
<tbody>
<tr v-for="data in mydata">
<td> data to appear in this section</td>
</tr>
</tbody>
</table>
您需要执行以下步骤:
app
命名为参考mydata
的数组填充数据属性>{{ data }}
内的td
调用项目的内容var appVM = new Vue({
el: '#app',
data: {mydata : ["cat", "mouse", "bird", "goat", "monkey", "giraffe", "cow", "donkey", "mice", "camel", "elephant", "bufalo", "jade", "zebra", "goose", "hen", "zat"]}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<table id="app" class="display table" width="100%">
<tbody>
<tr v-for="data in mydata">
<td> {{ data }}</td>
</tr>
</tbody>
</table>