vue初体验
<!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">
<h1>系统名称:{{title}}</h1>
<ul>
<li v-for="(item, i) in products">
商品名称:{{item.name}}
<button @click="changeStock(item,item.stock - 1)">-</button>
<span v-if="item.stock>0">库存:{{item.stock}}</span>
<span v-else>无货</span>
<button @click="changeStock(item,item.stock + 1)">+</button>
<button @click="remove(i)">删除</button>
</li>
</ul>
</div>
<script src="./vue.min.js"></script>
<script>
var vm = new Vue({
el: "#app",
data: {
title: "库存管理系统",
products: [
{ name: "iphone", stock: 10 },
{ name: "xiaomi", stock: 20 },
{ name: "huawei", stock: 30 },
],
},
methods: {
remove(i) {
this.products.splice(i, 1);
},
changeStock(product, newStock) {
if (newStock < 0) {
newStock = 0;
}
product.stock = newStock;
},
},
});
</script>
</body>
</html>