vue學習10 v-for指令
v-for指令的作用是:根據數據生成列表結構
數組經常和v-for結合使用,語法是(item,index) in 數據
數組長度的更新會同步到頁面上,是響應式更新
練習代碼:
<!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="app">
<input type="button" value="添加數據" @click="add">
<input type="button" value="移除數據" @click="remove">
<ul>
<li v-for = "(it,index) in arr">
{{index+1}} 俠客小飛學習:{{it}}
</li>
</ul>
<h2 v-for = "item in vegetables" v-bind:title="item.name">
{{ item.name }}
</h2>
</div>
<script src="js/vue.js"></script>
<script>
var app = new Vue({
el:"#app",
data: {
arr: ["C語言","java","python","html"],
vegetables:[
{name:"土豆肉絲蓋飯"},
{name:"韭菜雞蛋蓋飯"},
]
},
methods:{
add: function(){
this.vegetables.push({name:"西紅柿雞蛋蓋飯"});
},
remove: function(){
this.vegetables.shift();
}
},
})
</script>
</body>
</html>
運行效果: