Vue中的列表渲染
遍历数组和遍历对象
遍历数组
最外层有没要div
<template></template>与<div></div>的区别
<template>不会有占位符<div>会有
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src='./vue.js'></script>
</head>
<body>
<div id="app">
<div v-for="(item, index) of list"
:key="item.id">
<div>
{
{item.text}} ---- {
{index}}
</div>
<span>
{
{item.text}}
</span>
</div>
</div>
<script>
var vm = new Vue({
el: "#app",
data: {
list: [{
id: "010120201",
text: "hello"
}, {
id: "010120202",
text: "Dell"
}, {
id: "010120203",
text: "Lee"
}]
}
})
</script>
</body>
</html>
遍历对象
(item, key, index) 是一种函数。
<div id="app">
<div v-for="(item, key, index) of userInfo">
{
{item}} --- {
{key}} -- {
{index}}
</div>
</div>
<script>
var vm = new Vue({
el: "#app",
data: {
userInfo: {
name: "Dell",
age: 28,
gender: "male",
salary: "secret"
}
}
})
</script>