vue实现商城功能代码
时间: 2024-06-08 13:07:36 浏览: 12
实现一个完整的商城功能需要多个页面和组件,这里只提供一个简单的示例代码,仅供参考。
首先,在 Vue 中使用 `vue-router` 实现多页面路由切换:
```javascript
import Vue from 'vue'
import Router from 'vue-router'
import Home from '@/components/Home'
import Products from '@/components/Products'
import Cart from '@/components/Cart'
Vue.use(Router)
export default new Router({
routes: [
{
path: '/',
name: 'Home',
component: Home
},
{
path: '/products',
name: 'Products',
component: Products
},
{
path: '/cart',
name: 'Cart',
component: Cart
}
]
})
```
然后,创建三个组件 `Home`、`Products`、`Cart`,分别对应商城的首页、商品列表页和购物车页面。
在 `Home` 组件中,可以展示一些商品推荐、促销信息等:
```html
<template>
<div class="home">
<h2>商品推荐</h2>
<ul>
<li v-for="(product, index) in products" :key="index">
<router-link :to="{ name: 'Products', query: { category: product.category } }">{{ product.name }}</router-link>
</li>
</ul>
<h2>促销信息</h2>
<p>{{ promotion }}</p>
</div>
</template>
<script>
export default {
data () {
return {
products: [
{ name: '手机', category: '电子产品' },
{ name: '电脑', category: '电子产品' },
{ name: '衣服', category: '服装' },
{ name: '鞋子', category: '服装' }
],
promotion: '双11购物节,全场8折'
}
}
}
</script>
```
在 `Products` 组件中,可以展示某个分类下的商品列表,并提供加入购物车的功能:
```html
<template>
<div class="products">
<h2>{{ category }}商品列表</h2>
<ul>
<li v-for="(product, index) in products" :key="index">
<span>{{ product.name }}</span>
<span>¥{{ product.price }}</span>
<button @click="addToCart(product)">加入购物车</button>
</li>
</ul>
</div>
</template>
<script>
export default {
data () {
return {
category: '',
products: [
{ name: 'iPhone', category: '电子产品', price: 6999 },
{ name: 'iPad', category: '电子产品', price: 3999 },
{ name: 'T恤', category: '服装', price: 99 },
{ name: '牛仔裤', category: '服装', price: 199 }
]
}
},
methods: {
addToCart (product) {
this.$store.dispatch('addToCart', product)
}
},
mounted () {
this.category = this.$route.query.category
}
}
</script>
```
在 `Cart` 组件中,可以展示购物车中的商品列表,并提供修改数量和删除商品的功能:
```html
<template>
<div class="cart">
<h2>购物车</h2>
<ul>
<li v-for="(item, index) in cart" :key="index">
<span>{{ item.product.name }}</span>
<button @click="decreaseQuantity(item)">-</button>
<span>{{ item.quantity }}</span>
<button @click="increaseQuantity(item)">+</button>
<button @click="removeFromCart(item)">删除</button>
</li>
</ul>
<p>总价:¥{{ totalPrice }}</p>
</div>
</template>
<script>
export default {
computed: {
cart () {
return this.$store.state.cart
},
totalPrice () {
return this.$store.getters.totalPrice
}
},
methods: {
increaseQuantity (item) {
this.$store.dispatch('increaseQuantity', item)
},
decreaseQuantity (item) {
this.$store.dispatch('decreaseQuantity', item)
},
removeFromCart (item) {
this.$store.dispatch('removeFromCart', item)
}
}
}
</script>
```
以上代码仅是一个简单的示例,实际的商城功能需要考虑更多的细节问题,例如登录、支付、订单管理等。同时,也需要使用 Vuex 等状态管理工具来管理购物车等状态。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)