用node.js和vue3搭建一个超市商品信息管理系统项目,要有查询,修改,删除功能,给出相关代码框架。
时间: 2024-03-25 22:37:42 浏览: 18
好的,下面是一个简单的示例代码框架,供您参考:
后端API接口:
```javascript
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
// 使用body-parser中间件解析请求参数
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// 商品列表
let products = [
{ id: 1, name: '商品1', price: 100 },
{ id: 2, name: '商品2', price: 200 },
{ id: 3, name: '商品3', price: 300 },
];
// 查询商品列表
app.get('/api/products', (req, res) => {
res.json(products);
});
// 查询单个商品
app.get('/api/products/:id', (req, res) => {
const id = parseInt(req.params.id);
const product = products.find(p => p.id === id);
if (product) {
res.json(product);
} else {
res.status(404).json({ message: '商品不存在' });
}
});
// 添加商品
app.post('/api/products', (req, res) => {
const product = req.body;
product.id = products.length + 1;
products.push(product);
res.json(product);
});
// 修改商品
app.put('/api/products/:id', (req, res) => {
const id = parseInt(req.params.id);
const product = products.find(p => p.id === id);
if (product) {
Object.assign(product, req.body);
res.json(product);
} else {
res.status(404).json({ message: '商品不存在' });
}
});
// 删除商品
app.delete('/api/products/:id', (req, res) => {
const id = parseInt(req.params.id);
const index = products.findIndex(p => p.id === id);
if (index !== -1) {
products.splice(index, 1);
res.json({ message: '删除成功' });
} else {
res.status(404).json({ message: '商品不存在' });
}
});
// 启动服务器
app.listen(3000, () => {
console.log('Server started on port 3000');
});
```
前端Vue3项目:
```html
<template>
<div>
<!-- 商品列表 -->
<table>
<thead>
<tr>
<th>ID</th>
<th>名称</th>
<th>价格</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr v-for="product in products" :key="product.id">
<td>{{ product.id }}</td>
<td>{{ product.name }}</td>
<td>{{ product.price }}</td>
<td>
<button @click="editProduct(product)">编辑</button>
<button @click="deleteProduct(product)">删除</button>
</td>
</tr>
</tbody>
</table>
<!-- 添加/修改商品的弹窗 -->
<div v-if="showDialog">
<h3 v-if="selectedProduct">修改商品</h3>
<h3 v-else>添加商品</h3>
<form>
<div>
<label for="name">名称:</label>
<input type="text" id="name" v-model="product.name">
</div>
<div>
<label for="price">价格:</label>
<input type="number" id="price" v-model="product.price">
</div>
<button type="button" @click="saveProduct">保存</button>
<button type="button" @click="cancelProduct">取消</button>
</form>
</div>
</div>
</template>
<script>
import axios from 'axios';
export default {
data() {
return {
products: [],
selectedProduct: null,
product: {
name: '',
price: 0,
},
showDialog: false,
};
},
mounted() {
this.loadProducts();
},
methods: {
// 加载商品列表
loadProducts() {
axios.get('/api/products').then(res => {
this.products = res.data;
});
},
// 编辑商品
editProduct(product) {
this.selectedProduct = product;
this.product = { ...product };
this.showDialog = true;
},
// 删除商品
deleteProduct(product) {
axios.delete(`/api/products/${product.id}`).then(res => {
this.loadProducts();
});
},
// 保存商品
saveProduct() {
if (this.selectedProduct) {
// 修改商品
axios.put(`/api/products/${this.selectedProduct.id}`, this.product).then(res => {
this.selectedProduct = null;
this.product = { name: '', price: 0 };
this.showDialog = false;
this.loadProducts();
});
} else {
// 添加商品
axios.post('/api/products', this.product).then(res => {
this.product = { name: '', price: 0 };
this.showDialog = false;
this.loadProducts();
});
}
},
// 取消编辑商品
cancelProduct() {
this.selectedProduct = null;
this.product = { name: '', price: 0 };
this.showDialog = false;
},
},
};
</script>
```
需要注意的是,这只是一个简单的示例代码框架,实际项目中还需要进行更多的优化和完善,例如数据验证、错误处理、权限管理等。