优化这段代码<!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>图书购物车</title> <style> </style> <script src="js/vue.js"></script> </head> <body> <div id="demo"> <table border="1"> <tr> <td></td> <td>书籍名称</td> <td>出版日期</td> <td>价格</td> <td>购买数量</td> <td>操作</td> </tr> <tr> <td></td> <td>{{books1.name}}</td> <td>{{books1.date}}</td> <td>¥{{books1.price}}</td> <td><button @click="down(books1)">-</button>{{books4.count}}<button @click="up(books1)">+</button></td> <td><button @click="del">移除</button></td> </tr> <tr> <td></td> <td>{{books2.name}}</td> <td>{{books2.date}}</td> <td>¥{{books2.price}}</td> <td> <button @click="down(books2)">-</button>{{books4.count}}<button @click="up(books2)">+</button> </td> <td><button @click="del">移除</button></td> </tr> <tr> <td></td> <td>{{books3.name}}</td> <td>{{books3.date}}</td> <td>¥{{books3.price}}</td> <td> <button @click="down(books3)">-</button>{{books4.count}}<button @click="up(books3)">+</button> </td> <td><button @click="del">移除</button></td> </tr> <tr> <td></td> <td>{{books4.name}}</td> <td>{{books4.date}}</td> <td>¥{{books4.price}}</td> <td> <button @click="down(books4)">-</button>{{books4.count}}<button @click="up(books4)">+</button> </td> <td><button @click="del">移除</button></td> </tr> </table> <div>总价: ¥{{sum}}</div> </div> <!-- js部分 --> <script> const vm = new Vue({ el: "#demo", data: { books1: { name: '《算法导论》', date: '2006-9', price: 85.00, count: 1 }, books2: { name: '《UNIX编程艺术》', date: '2006-2', price: 59.00, count: 1 }, books3: { name: '《编程珠玑》', date: '2008-10', price: 39.00, count: 1 }, books4: { name: '《代码大全》', date: '2006-3', price: 128.00, count: 1 } }, computed: { sum () { return this.books1.price * this.books1.count + this.books2.price * this.books2.count + this.books3.price * this.books3.count + this.books4.price * this.books4.count } }, methods: { down (books1) { this.books1.count = books1.count - 1; } } }) </script> </body> </html>
时间: 2023-06-05 14:07:13 浏览: 136
HTML-CSS.md
优化建议:
1. 使用数组来存储书籍信息,可以避免重复的代码和变量
2. 使用计算属性来计算总价,避免重复计算
3. 将操作封装成方法,并使用可复用的组件来渲染每一行
优化后的代码如下:
<!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>图书购物车</title>
<style>
</style>
<script src="js/vue.js"></script>
</head>
<body>
<div id="demo">
<table border="1">
<tr>
<td></td>
<<td>书籍名称</td>
<td>出版日期</td>
<td>价格</td>
<td>购买数量</td>
<td>操作</td>
</tr>
<book-row v-for="(book, index) in books"
:book="book"
:index="index"
:key="index"
@change-count="changeCount"
@remove-book="removeBook" />
</table>
<div>总价: ¥{{totalPrice}}</div>
</div>
<!-- js部分 -->
<script>
Vue.component('book-row', {
props: ['book', 'index'],
methods: {
down() {
if (this.book.count > 1) {
this.book.count--;
this.$emit('change-count');
}
},
up() {
this.book.count++;
this.$emit('change-count');
},
remove() {
this.$emit('remove-book', this.index);
}
},
template: `
<tr>
<td></td>
<td>{{book.name}}</td>
<td>{{book.date}}</td>
<td>¥{{book.price}}</td>
<td>
<button @click="down">-</button>
{{book.count}}
<button @click="up">+</button>
</td>
<td>
<button @click="remove">移除</button>
</td>
</tr>
`
});
const vm = new Vue({
el: "#demo",
data() {
return {
books: [
{ name: '书籍1', date: '2022-01-01', price: 30, count: 1 },
{ name: '书籍2', date: '2022-01-01', price: 40, count: 1 },
{ name: '书籍3', date: '2022-01-01', price: 50, count: 1 },
{ name: '书籍4', date: '2022-01-01', price: 60, count: 1 }
]
};
},
methods: {
changeCount() {
// 重新计算总价
},
removeBook(index) {
this.books.splice(index, 1);
// 重新计算总价
},
},
computed: {
totalPrice() {
return this.books.reduce((acc, cur) => acc + cur.price * cur.count, 0);
}
}
});
</script>
</body>
</html>
阅读全文