当前位置:首页 > 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 组件中实现购物车功能,包含商品列表、添加商品和结算逻辑。

商品列表展示

使用 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 实现添加商品的弹窗:

<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 美化购物车界面:

vue用element实现购物

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

功能扩展

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

标签: vueelement
分享给朋友:

相关文章

vue实现表格多行修改

vue实现表格多行修改

实现多行编辑表格 在Vue中实现表格的多行编辑功能,可以通过以下方法完成。这里以Element UI的表格组件为例,但思路适用于其他UI库或原生实现。 数据准备 定义表格数据和编辑状态管理变量:…

vue实现双折线图

vue实现双折线图

实现双折线图的步骤 安装必要的依赖库(如 ECharts 或 Chart.js),这里以 ECharts 为例: npm install echarts --save 在 Vue 组件中引入 ECh…

vue 实现

vue 实现

Vue 实现的基本步骤 安装 Vue.js 可以通过 npm 或 CDN 引入。使用 npm 安装适合大型项目,CDN 适合快速原型开发。 npm install vue 在 HTML 文件中引入…

vue实现主页

vue实现主页

Vue 实现主页的基本步骤 创建一个 Vue 主页通常涉及以下几个关键步骤,从项目初始化到页面布局和功能实现。 初始化 Vue 项目 使用 Vue CLI 或 Vite 初始化项目。Vue CLI…

vue实现截图

vue实现截图

Vue 实现截图的方法 使用 html2canvas 库 安装 html2canvas 库: npm install html2canvas 在 Vue 组件中使用: import html2ca…

实现简易vue

实现简易vue

实现简易 Vue 的核心思路 Vue 的核心功能包括数据响应式、模板编译和虚拟 DOM。以下是一个简易实现方案,涵盖核心功能模块。 数据响应式系统 通过 Object.defineProperty…