用vant和vue写一个商品发布和购买页面
时间: 2024-04-25 11:27:21 浏览: 255
好的,下面是一个简单的商品发布和购买页面的示例,代码基于Vant UI库和Vue.js框架实现。
首先,创建一个Vue.js项目,并安装Vant UI库:
```
vue create my-project
cd my-project
npm install vant
```
接着,在`src/main.js`文件中引入Vant UI库和样式:
```javascript
import { createApp } from 'vue'
import App from './App.vue'
import 'vant/lib/index.css'
import { Button, Form, Field, Uploader, Dialog, SubmitBar, Card } from 'vant'
const app = createApp(App)
app.use(Button)
app.use(Form)
app.use(Field)
app.use(Uploader)
app.use(Dialog)
app.use(SubmitBar)
app.use(Card)
app.mount('#app')
```
然后,在`src/App.vue`文件中编写商品发布和购买页面的代码:
```html
<template>
<div class="container">
<van-form v-model="form" label-width="80px">
<van-field v-model="form.name" label="商品名称" required />
<van-field v-model="form.description" label="商品描述" type="textarea" rows="3" required />
<van-field v-model="form.price" label="商品价格" type="number" required />
<van-field v-model="form.category" label="商品分类" required />
<van-uploader v-model="form.images" label="商品图片" multiple accept="image/*" :max-count="3" />
<van-button type="primary" @click="submitForm">发布</van-button>
</van-form>
<van-card v-for="(item, index) in goods" :key="index" :title="item.name" :price="item.price" :desc="item.description" :thumb="item.images[0].url">
<template #footer>
<van-button type="primary" @click="showDialog(item)">购买</van-button>
</template>
</van-card>
<van-dialog v-model="showBuyDialog" title="购买确认" :confirm-button-text="'确认购买(¥' + selectedGoods.price.toFixed(2) + ')'" @confirm="buyGoods">
<p>商品名称:{{ selectedGoods.name }}</p>
<p>商品价格:¥{{ selectedGoods.price.toFixed(2) }}</p>
<p>商品描述:{{ selectedGoods.description }}</p>
<p>商品分类:{{ selectedGoods.category }}</p>
<p>商品图片:</p>
<van-image v-for="(image, index) in selectedGoods.images" :key="index" :src="image.url" width="80" height="80" fit="cover" />
</van-dialog>
<van-submit-bar :price="totalPrice" button-text="去结算" @submit="submitOrder" />
</div>
</template>
<script>
import { reactive } from 'vue'
export default {
name: 'App',
setup() {
const form = reactive({
name: '',
description: '',
price: '',
category: '',
images: []
})
const goods = reactive([
{
name: '商品1',
price: 100,
description: '商品1的描述',
category: '分类1',
images: [
{ url: 'https://img.yzcdn.cn/vant/ipad.jpeg' },
{ url: 'https://img.yzcdn.cn/vant/ipad.jpeg' },
{ url: 'https://img.yzcdn.cn/vant/ipad.jpeg' }
]
},
{
name: '商品2',
price: 200,
description: '商品2的描述',
category: '分类2',
images: [
{ url: 'https://img.yzcdn.cn/vant/ipad.jpeg' },
{ url: 'https://img.yzcdn.cn/vant/ipad.jpeg' }
]
}
])
const selectedGoods = reactive({
name: '',
price: '',
description: '',
category: '',
images: []
})
let showBuyDialog = false
const totalPrice = computed(() => {
return selectedGoods.price
})
const submitForm = () => {
console.log(form)
// TODO: 发送表单数据到后端API
}
const showDialog = (item) => {
selectedGoods.name = item.name
selectedGoods.price = item.price
selectedGoods.description = item.description
selectedGoods.category = item.category
selectedGoods.images = item.images
showBuyDialog = true
}
const buyGoods = () => {
console.log(selectedGoods)
// TODO: 发送购买请求到后端API
showBuyDialog = false
}
const submitOrder = () => {
console.log('提交订单')
// TODO: 提交订单到后端API
}
return {
form,
goods,
selectedGoods,
showBuyDialog,
totalPrice,
submitForm,
showDialog,
buyGoods,
submitOrder
}
}
}
</script>
<style>
.container {
padding: 16px;
}
</style>
```
在上面的代码中,我们使用`van-form`组件和`van-field`组件实现商品发布表单,使用`van-uploader`组件实现图片上传功能,使用`van-card`组件展示已发布的商品列表。在购买商品时,我们使用`van-dialog`组件展示商品详情,并使用`van-submit-bar`组件实现结算功能。
注意:在实际开发中,你需要根据实际需求对代码进行调整和优化。
阅读全文