v-for遍历对象
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="app">
<ul>
<li v-for="item in message">{{item}}</li>
</ul>
<!-- 获取key和value (item,key)顺序不能变-->
<ul>
<li v-for="(item,key) in message">{{key}}-{{item}}</li>
</ul>
<!-- 解决互用问题-->
<ul>
<li v-for="(item,key) in message" :key="item">{{key}}-{{item}}</li>
</ul>
<!-- 获取index和key和value (item,key,index)顺序不能变--->
<ul>
<li v-for="(item,key,index) in message">{{index}}-{{key}}-{{item}}</li>
</ul>
</div>
<script src="../vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
message: {
name: "LSF",
age: 20,
height: 175
}
}
})
</script>
</body>
</html>
v-for遍历数组
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="divrel">
<ul>
<li v-for="item in movies">{{item}}</li>
</ul>
<!-- 在遍历时获取索引值 -->
<ul>
<li v-for="(item, index) in movies">{{index+1}}-{{item}}</li>
</ul>
</div>
</body>
<script src="../vue.js"></script>
<script>
const app = new Vue({
el: '#divrel',
data: {
movies:['星际穿越','大话西游','少年派','盗梦空间']
}
});
app.movies.push('海王');
</script>
</html>