用vue和iview做了一个购物车小页面

<template>

  <div id="app">

    <div class="container">

      <h1>购物车</h1>

      <Table border :columns="columns1" :data="data1">

        <template slot-scope="{ row }" slot="pic">

          <!-- <strong>{{ row.name }}</strong> -->

          <img :src="row.pic" alt="" />

        </template>

        <template slot-scope="{ row }" slot="total">

          <span>{{ computedTotal(row.price, row.count) }}</span>

        </template>

        <template slot-scope="{ row, index }" slot="action">

          <Button type="error" @click="remove(index)">删除</Button>

        </template>

      </Table>

      <div class="footer">

        <h1>总计:{{ total }}</h1>

        <p>

          <Button type="success" @click="submit">提交</Button>

        </p>

      </div>

    </div>

  </div>

</template>

<script>

import bignumber from 'bignumber.js'

export default {

  name: 'App',

  data() {

    return {

      columns1: [

        {

          title: '序号',

          key: 'name',

          type: 'index',

        },

        {

          title: '图片',

          key: 'pic',

          slot: 'pic',

        },

        {

          title: '单价',

          key: 'price',

        },

        {

          title: '商品总价',

          slot: 'total',

        },

        {

          title: '数量',

          key: 'count',

          render: (h, params) => {

            return h('div', [

              h('Input-number', {

                props: {

                  max: 100,

                  min: 0,

                  step: 1,

                  value: params.row.count,

                },

                on: {

                  'on-change': (value) => {

                    this.data1[params.index].count = value

                  },

                },

              }),

            ])

          },

        },

        {

          title: '操作',

          key: '',

          slot: 'action',

        },

      ],

      data1: [

        {

          id: '0',

          name: '王小明',

          pic: './assets/j1.jpg',

          price: '34.63',

          count: 2,

        },

        {

          id: '1',

          name: '张小刚',

          pic: './assets/j1.jpg',

          price: '34.63',

          count: 0,

        },

        {

          id: '2',

          name: '李小红',

          pic: './assets/j1.jpg',

          price: '34.63',

          count: 0,

        },

        {

          id: '3',

          name: '周小伟',

          pic: './assets/j1.jpg',

          price: '34.63',

          count: 0,

        },

      ],

    }

  },

  methods: {

    computedTotal(price, count) {

      return new bignumber(price).multipliedBy(count).toFixed(2)

    },

    remove(index) {

      this.$Modal.confirm({

        title: '提示',

        content: '确认删除这条数据吗?',

        onOk: () => {

          this.data1.splice(index, 1)

        },

        onCancel: () => {

          this.$Message.info('已取消')

        },

      })

    },

    submit() {

      const req = this.data1.map((item) => ({

        id: item.id,

        count: item.count,

      }))

      console.log(req)

    },

  },

  computed: {

    total() {

      const num = this.data1.reduce((a, b) => {

        return new bignumber(a).plus(

          new bignumber(b.count).multipliedBy(b.price),

        )

      }, 0)

      return new bignumber(num).toFixed(2)

    },

  },

}

</script>

<style>

#app {

  margin-top: 60px;

}

.container {

  width: 1224px;

  margin: 0 auto;

}

img {

  width: 30px;

  height: 20px;

}

.footer {

  right: 0;

}

</style>

 

Vue3中创建一个购物车页面,我们可以采用组件化的方式来构建。以下是步骤概述: 1. **初始化项目**:首先,确保你已经安装了Vue CLI并创建一个新的Vue3项目。 2. **设计数据模型**:定义购物车的数据结构,比如`CartItem`对象,包含商品ID、名称、价格等信息,以及一个`list`数组来存储所有商品。 ```js // models/CartItem.js export default { data() { return { cartItems: [], }; }, }; ``` 3. **创建组件**:创建一个名为`Cart.vue`的组件,展示购物车列表和操作功能(如添加、删除、修改数量)。 ```html <!-- Cart.vue --> <template> <div class="cart"> <ul> <li v-for="(item, index) in cartItems" :key="index"> <span>{{ item.name }} - {{ item.price }}</span> <button @click="removeItem(index)">Remove</button> <input type="number" v-model.number="item.quantity" min="1" /> <button @click="updateItem(index)">Update</button> </li> </ul> <button @click="addItem">Add Item</button> </div> </template> <script> import CartItem from '@/models/CartItem'; export default { components: { CartItem }, data() { return { newCartItem: { ...CartItem.data(), quantity: 1 }, // 新商品预设项 }; }, methods: { addItem() { this.cartItems.push(this.newCartItem); this.newCartItem = { ...CartItem.data(), quantity: 1 }; // 清空新商品项 }, removeItem(index) { this.cartItems.splice(index, 1); }, updateItem(index) { const updatedItem = { ...this.cartItems[index], quantity: this.cartItems[index].quantity + 1 }; this.cartItems[index] = updatedItem; }, }, }; </script> ``` 4. **路由配置**:在`router/index.js`中添加购物车的路由,跳转到`Cart.vue`组件。 ```js // router/index.js import { createRouter, createWebHashHistory } from 'vue-router'; import Cart from './views/Cart.vue'; const routes = [ // ... { path: '/cart', component: Cart, }, ]; const router = createRouter({ history: createWebHashHistory(), routes, }); export default router; ``` 5. **导航到购物车**:在需要的地方(如商品详情页),添加链接或者按钮,通过`this.$router.push('/cart')`导航到购物车。 以上就是基本的购物车页面Vue3中的实现。你可以根据实际需求增加更多功能,例如显示总价、优惠券应用等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值