当前位置:首页 > VUE

vue用element实现购物

2026-02-23 02:43:52VUE

使用 Vue 和 Element UI 实现购物功能

安装依赖

确保项目中已安装 Vue 和 Element UI。若未安装,可通过以下命令安装:

npm install element-ui

引入 Element UI

main.js 中全局引入 Element UI:

import Vue from 'vue';
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';

Vue.use(ElementUI);

创建购物车组件

在 Vue 组件中实现购物车功能,包含商品列表、添加商品和结算逻辑。

vue用element实现购物

商品列表展示

使用 el-table 展示商品列表,支持选择商品和调整数量:

<template>
  <div>
    <el-table :data="products" border>
      <el-table-column prop="name" label="商品名称"></el-table-column>
      <el-table-column prop="price" label="价格"></el-table-column>
      <el-table-column label="数量">
        <template slot-scope="scope">
          <el-input-number v-model="scope.row.quantity" :min="1"></el-input-number>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="danger" @click="removeProduct(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

添加商品逻辑

通过 el-dialog 实现添加商品的弹窗:

vue用element实现购物

<el-button type="primary" @click="dialogVisible = true">添加商品</el-button>
<el-dialog title="添加商品" :visible.sync="dialogVisible">
  <el-form :model="newProduct">
    <el-form-item label="商品名称">
      <el-input v-model="newProduct.name"></el-input>
    </el-form-item>
    <el-form-item label="价格">
      <el-input v-model="newProduct.price" type="number"></el-input>
    </el-form-item>
  </el-form>
  <span slot="footer">
    <el-button @click="dialogVisible = false">取消</el-button>
    <el-button type="primary" @click="addProduct">确认</el-button>
  </span>
</el-dialog>

结算功能

计算总价并提供结算按钮:

<div class="total">
  <span>总价: {{ totalPrice }}</span>
  <el-button type="success" @click="checkout">结算</el-button>
</div>

数据与逻辑实现

script 部分定义数据和方法:

export default {
  data() {
    return {
      products: [
        { name: '商品1', price: 100, quantity: 1 },
        { name: '商品2', price: 200, quantity: 1 }
      ],
      newProduct: { name: '', price: 0, quantity: 1 },
      dialogVisible: false
    };
  },
  computed: {
    totalPrice() {
      return this.products.reduce((sum, item) => sum + item.price * item.quantity, 0);
    }
  },
  methods: {
    addProduct() {
      this.products.push({ ...this.newProduct });
      this.dialogVisible = false;
      this.newProduct = { name: '', price: 0, quantity: 1 };
    },
    removeProduct(product) {
      this.products = this.products.filter(p => p !== product);
    },
    checkout() {
      this.$message.success(`结算成功,总价: ${this.totalPrice}`);
    }
  }
};

样式优化

通过 CSS 美化购物车界面:

.total {
  margin-top: 20px;
  text-align: right;
  font-size: 18px;
}

功能扩展

  1. 本地存储:使用 localStorage 持久化购物车数据。
  2. 接口集成:通过 API 获取商品列表和提交订单。
  3. 用户验证:结合后端实现用户登录和订单管理。

标签: vueelement
分享给朋友:

相关文章

vue菜单实现

vue菜单实现

Vue 菜单实现方法 使用 Element UI 的菜单组件 Element UI 提供了现成的菜单组件,适合快速实现导航菜单。安装 Element UI 后,可以直接使用 el-menu 相关组件。…

vue页面实现

vue页面实现

Vue 页面实现方法 创建 Vue 组件 使用 Vue 单文件组件(SFC)方式创建页面,文件扩展名为 .vue。一个基本的 Vue 组件包含三个部分:模板(template)、脚本(script)和…

computed vue 实现

computed vue 实现

computed 在 Vue 中的基本用法 Vue 的 computed 属性用于声明依赖于其他数据的计算属性。当依赖的数据变化时,计算属性会自动重新计算并缓存结果。 export defaul…

vue实现数组

vue实现数组

Vue 实现数组操作的方法 在 Vue 中,数组的操作需要特别注意响应式的问题。Vue 提供了一些方法来确保数组的变动能够触发视图更新。 响应式数组方法 Vue 对以下数组方法进行了封装,确保它们能…

vue实现验证

vue实现验证

Vue 表单验证实现方法 在Vue中实现表单验证可以通过多种方式完成,以下是常见的几种方法: 使用Vuelidate库 Vuelidate是一个轻量级的Vue表单验证库,安装后可以通过简单的配置实现…

vue实现影院

vue实现影院

Vue实现影院功能的基本思路 使用Vue框架开发影院功能需要结合前端组件化开发、状态管理以及后端API交互。以下是关键实现步骤: 影院页面布局设计 影院页面通常分为顶部导航栏、影院列表区域和底部导航…