<!DOCTYPE html>
<html lang="en" xmlns:v-on="http://www.w3.org/1999/xhtml" xmlns:v-bind="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.active{
border:1px solid red;
}
</style>
</head>
<body>
<div id="app">
<input type="button" value="添加" @click="add">
<input type="button" value="移除数据" @click="remove">
<ul>
<li v-for="(item,index) in arr">
index:{{index + 1}}
emotion:{{item}}
</li>
</ul>
<h2 v-for="(item,index) in stress">stress:{{item}}</h2>
<h2 v-for="(item,index) in vegetables" v-bind:title="item.name"> {{index}} {{item.name}}</h2> <!--index 不能缩写只能写成index 但是item 可以缩写成it-->
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
//创建vue实例
var app = new Vue({
el:"#app",
data:{
arr:["忧伤","难过","开心","愤怒"],
stress:["不到工作","三观不合","我想要女朋友"],
vegetables:[
{name:"鸡蛋"},
{name:"西红柿"}
]
},
methods:{
add:function(){
this.vegetables.push({
name:"花菜炒蛋"//后面添加一个属性
})
},
remove:function(){
this.vegetables.shift();//左侧删除
}
},
})
</script>
</body>
</html>
Vue 添加元素 删除元素
最新推荐文章于 2024-09-12 12:37:49 发布