当前位置:首页 > 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 美化购物车界面:

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

功能扩展

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

vue用element实现购物

标签: vueelement
分享给朋友:

相关文章

vue 页签实现

vue 页签实现

Vue 页签实现方法 在 Vue 中实现页签功能通常涉及动态组件、路由或状态管理。以下是几种常见的实现方式: 使用动态组件 通过 Vue 的 <component> 动态组件结合 v-…

vue实现关键词轮播

vue实现关键词轮播

Vue 实现关键词轮播 基础实现方案 使用 Vue 的 v-for 和 setInterval 实现基础轮播效果: <template> <div class="keywor…

vue实现密码框的包

vue实现密码框的包

Vue 密码框实现方案 在 Vue 中实现密码框功能,可以通过原生 HTML 结合 Vue 指令或使用第三方组件库完成。以下是几种常见方案: 原生 HTML + Vue 双向绑定 <temp…

vue原型实现

vue原型实现

Vue 原型实现的关键点 Vue.js 的原型系统是其响应式机制的核心部分,通过原型链实现数据的动态绑定和依赖收集。以下是实现 Vue 原型的主要技术要点。 数据响应式原理 Vue 通过 Obje…

vue 实现弹幕

vue 实现弹幕

vue 实现弹幕的方法 使用 CSS 动画和动态渲染 在 Vue 中实现弹幕效果,可以通过动态渲染弹幕元素并结合 CSS 动画实现。以下是具体步骤: 数据准备 创建一个数组存储弹幕数据,每条弹幕…

vue实现删除

vue实现删除

Vue 实现删除功能的方法 在 Vue 中实现删除功能通常涉及以下几个步骤: 数据绑定与列表渲染 使用 v-for 指令渲染列表数据,并为每个项绑定唯一标识符(如 id)。例如: <…