Vue.js 案例——商品管理的bootstrap.css代码
时间: 2024-09-30 15:14:28 浏览: 86
Vue.js 结合 Bootstrap 可以创建一个简洁美观的商品管理界面。这里是一个基本的例子,展示了如何使用 Bootstrap CSS 样式来美化商品列表和表单部分:
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>商品管理</title>
<!-- 引入Bootstrap CSS -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css">
<!-- 引入Vue.js 和 Axios (用于API请求) -->
<script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</head>
<body>
<div id="app">
<table class="table table-striped">
<thead>
<tr>
<th>ID</th>
<th>名称</th>
<th>价格</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr v-for="(product, index) in products" :key="index">
<td>{{ product.id }}</td>
<td>{{ product.name }}</td>
<td>{{ product.price }}</td>
<td>
<button type="button" class="btn btn-primary" @click="editProduct(product)">编辑</button>
<button type="button" class="btn btn-danger" @click="deleteProduct(product)">删除</button>
</td>
</tr>
</tbody>
</table>
<!-- 添加产品模态框 -->
<div class="modal fade" id="addProductModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<form @submit.prevent="addProduct">
<input type="text" v-model="newProductName" class="form-control" placeholder="名称">
<input type="number" v-model.number="newProductPrice" class="form-control" placeholder="价格">
<button type="submit" class="btn btn-primary">添加</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button>
</form>
</div>
</div>
</div>
</div>
<script>
new Vue({
el: '#app',
data: {
products: [],
newProductName: '',
newProductPrice: 0,
},
methods: {
async fetchData() {
try {
const response = await axios.get('/api/products');
this.products = response.data;
} catch (error) {
console.error('Error fetching products:', error);
}
},
editProduct(product) {
// 编辑功能的逻辑...
},
deleteProduct(product) {
// 删除功能的逻辑...
this.fetchData(); // 刷新数据
},
addProduct() {
if (this.newProductName && this.newProductPrice > 0) {
// 发送添加产品的请求...
this.fetchData();
this.newProductName = '';
this.newProductPrice = 0;
}
},
mounted() {
this.fetchData();
},
},
});
</script>
</body>
</html>
```
阅读全文