vue实现购物车的小练习

yizhihongxing

Vue实现购物车的小练习是一个非常适合新手练手的小项目。 在这个练习中,我们将使用Vue.js框架来实现一个简单的购物车系统。下面是这个项目的完整攻略:

步骤1:准备项目

在开始之前,我们需要确保已经安装好了Vue.js库。我们可以通过以下方式进行安装:

npm install vue

步骤2:创建购物车组件

购物车组件是整个项目的核心,它需要完成的功能有:

  • 展示购物车中的商品列表
  • 提供增加/减少商品数量和删除商品的功能
  • 展示购物车中商品的总价和总数量

示例代码如下:

<template>
    <div>
        <div v-for="(product, index) in products" :key="index">
            {{ product.name }} - {{ product.price }}元
            <button @click="increment(index)">+</button>
            {{ product.quantity }}
            <button @click="decrement(index)">-</button>
            <button @click="removeProduct(index)">X</button>
        </div>
        <p>总价: {{ totalPrice }}元</p>
        <p>购买总数: {{ totalQuantity }}</p>
    </div>
</template>

<script>
export default {
    data() {
        return {
            products: [
                { name: "可乐", price: 5, quantity: 0 },
                { name: "矿泉水", price: 2, quantity: 0 },
                { name: "雪碧",  price: 4, quantity: 0 },
            ]
        }
    },
    computed: {
        totalPrice() {
            let total = 0;
            this.products.forEach(product => {
                total += product.price * product.quantity;
            });
            return total;
        },
        totalQuantity() {
            let total = 0;
            this.products.forEach(product => {
                total += product.quantity;
            });
            return total;
        }
    },
    methods: {
        increment(index) {
            this.products[index].quantity++;
        },
        decrement(index) {
            if (this.products[index].quantity > 0) {
                this.products[index].quantity--;
            }
        },
        removeProduct(index) {
            this.products.splice(index, 1);
        }
    }
}
</script>

步骤3:在页面中引入购物车组件

在我们使用购物车组件之前,需要将其引入到我们的App.vue组件中。示例代码如下:

<template>
    <div>
        <cart></cart>
    </div>
</template>

<script>
import Cart from './components/Cart.vue';
export default {
    components: {
        Cart
    }
}
</script>

步骤4:交互

现在,我们的购物车组件已经可以在页面上展示了,但是还不能交互。我们需要在页面中添加几个按钮,实现增加/减少商品数量和删除商品的功能。示例代码如下:

<template>
    <div>
        <div v-for="(product, index) in products" :key="index">
            {{ product.name }} - {{ product.price }}元
            <button @click="increment(index)">+</button>
            {{ product.quantity }}
            <button @click="decrement(index)">-</button>
            <button @click="removeProduct(index)">X</button>
        </div>
        <p>总价: {{ totalPrice }}元</p>
        <p>购买总数: {{ totalQuantity }}</p>
        <button @click="addProduct">添加商品</button>
    </div>
</template>

<script>
export default {
    data() {
        return {
            products: [
                { name: "可乐", price: 5, quantity: 0 },
                { name: "矿泉水", price: 2, quantity: 0 },
                { name: "雪碧",  price: 4, quantity: 0 },
            ]
        }
    },
    computed: {
        totalPrice() {
            let total = 0;
            this.products.forEach(product => {
                total += product.price * product.quantity;
            });
            return total;
        },
        totalQuantity() {
            let total = 0;
            this.products.forEach(product => {
                total += product.quantity;
            });
            return total;
        }
    },
    methods: {
        increment(index) {
            this.products[index].quantity++;
        },
        decrement(index) {
            if (this.products[index].quantity > 0) {
                this.products[index].quantity--;
            }
        },
        removeProduct(index) {
            this.products.splice(index, 1);
        },
        addProduct() {
            this.products.push({
                name: "新商品",
                price: 10,
                quantity: 0,
            });
        }
    }
}
</script>

以上就是Vue实现购物车的小练习的完整攻略,希望对您有帮助。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:vue实现购物车的小练习 - Python技术站

(0)
上一篇 2023年5月29日
下一篇 2023年5月29日

相关文章

  • vue-test-utils初使用详解

    Vue Test Utils初使用详解 Vue Test Utils是Vue.js官方提供的用于单元测试Vue.js组件的工具库。它提供的API可以让我们在测试组件时模拟真实的DOM操作和用户交互,并且能够很好地集成到常见的JavaScript测试工具中。本文将详细讲解Vue Test Utils的初步使用,希望能够帮助你更好地编写Vue.js组件的单元测试…

    Vue 2023年5月28日
    00
  • Vue中遍历数组的新方法实例详解

    下面我就为您详细讲解“Vue中遍历数组的新方法实例详解”。 介绍 在Vue 2.6.0版本以后,新增了一个数组方法v-for,它主要用于遍历一个数组并渲染每个数组元素。 v-for能够将一个数组映射为一组元素,并为每个元素执行一次模板,因此它的应用场景非常广泛,尤其在将复杂数据渲染到界面上时,更是体现了它的优势。 下面就重点介绍一下v-for在其中的应用。 …

    Vue 2023年5月28日
    00
  • 解析Vue2 dist 目录下各个文件的区别

    Vue2 是一款流行的 JavaScript 前端框架,它的 dist 目录下包含了多个文件,每个文件都有自己的职责和用途。下面我将详细讲解 Vue2 dist 目录下各个文件的区别。 vue.js vue.js 文件是最基本的 Vue2 库文件,包含了 Vue 的核心代码和各种插件。如果你只想使用 Vue 就可以将这个文件添加到你的 HTML 文件中,然后…

    Vue 2023年5月28日
    00
  • 在axios中使用params传参的时候传入数组的方法

    在axios中使用params传参时,如果需要传入数组参数,可以按照以下步骤来进行。 在调用axios.get或axios.post时,将参数放在params或data中,并设置paramsSerializer的值为Qs.stringify,即使用qs对数组参数进行序列化。 示例代码: import axios from ‘axios’; import Qs…

    Vue 2023年5月29日
    00
  • Vue2.0父子组件传递函数的教程详解

    Vue2.0 父子组件传递函数的教程详解 在Vue2.0中,通过props和$emit等特性,进行组件之间数据的传递和事件的通信变得非常方便。如何在Vue2.0中实现父子组件传递函数呢?本文将详细介绍该过程。 基本思路 父子组件传递函数的基本思路,是将一个函数作为props传递给子组件,在子组件中调用该函数,从而实现子组件的一些交互行为能够改变父组件中的状态…

    Vue 2023年5月28日
    00
  • Vue自定义事件(详解)

    Vue自定义事件(详解) 什么是自定义事件 Vue中除了系统事件(如:click、change、input等),用户也可以自定义事件。自定义事件可以通过$emit来触发,在组件树中向上传递数据,被其它组件接收并响应处理。 如何使用自定义事件 在父组件中触发自定义事件 示例1:利用$emit触发自定义事件 <template> <div&gt…

    Vue 2023年5月28日
    00
  • until封装watch常用逻辑简化代码写法

    我来详细讲解一下“until封装watch常用逻辑简化代码写法”的攻略。 什么是until until是Vue.js中一个常用的指令修饰符,它用于监听数据变化直到满足条件才执行操作。常用语法如下: <!– 监听value值变化,直到其等于一个值为9的时候才执行alert方法 –> <div v-on:click="alert(…

    Vue 2023年5月27日
    00
  • Vue父组件调用子组件函数实现

    下面是详细讲解如何通过Vue父组件调用子组件函数实现: 步骤一:创建子组件 在Vue中,我们通过Vue.component来创建一个组件。创建子组件的代码示例: Vue.component(‘child-component’, { methods: { childFunc() { console.log(‘子组件函数执行’) } } }) 在以上示例中,我们…

    Vue 2023年5月28日
    00
合作推广
合作推广
分享本页
返回顶部