【发布时间】:2022-07-05 18:08:17
【问题描述】:
我目前正在尝试开发一个总共有 6 个父视图的网上商店。
几乎每个父视图也有子组件“购物车”——购物车中当前产品的概览。 该组件始终显示在相应视图的右侧,并且其内容应该(显然)在路由更改之后保留。
现在我有点希望(不知道为什么)Vue.js 会简单地识别下一个视图中也使用了相同的子组件 因此,数据将被简单地保留 - 错误的想法:')。
所以看起来我必须在每个视图和现有信息中一遍又一遍地重新实现(或者更确切地说是复制粘贴)数据和逻辑
- 总价
- 购物车中的产品
- 数量
等等。一遍又一遍地从一个视图到另一个视图。
原则上,这个问题可以通过定义整个逻辑而不是在父组件中,而是在购物车组件本身中来相对容易地解决。但是,曾经有人对我说,好的编程风格的特点是让子组件保持“哑”,并将逻辑保持在父组件中。
出于这个原因,我希望你能告诉我如何以最有效的方式解决这个问题,将子组件的现有信息从一个父视图移动到另一个父视图,并能够实现购物逻辑以最有效的方式购物车。
提前致谢!
代码:
购物车:
<template>
<div id="shopping_cart">
<div id="wrapper">
<div id="cart_info">
<p id="product_count">{{totalQuantity}} Produkt<span v-if="totalQuantity !== 1">e</span></p>
<p id="total_price">{{toEuro(totalPrice)}} €</p>
<router-link to="/Warenkorb">
<button id="cart_btn">Zum Warenkorb</button>
</router-link>
<button @click="deleteCart()" id="delete_cart_btn"><img src="../../assets/img/Icons/Delete_Icon.png"></button>
<hr>
</div>
<div id="cart_products">
<table>
<tr v-for="(cart_item, index) in cart_items" class="item">
<div id="img_wrapper">
<img id="cart_item_img" :src="getImage(cart_item.getProduct().img)">
</div>
<p class="cart_text" id="cart_product_description">{{cart_item.getProduct().name}}</p>
<p class="cart_text" id="cart_product_quantity"> {{cart_item.getProduct().quantity}}</p>
<p class="cart_text" id="cart_product_price_per_kg"> {{cart_item.getProduct().price_per_kg}}</p>
<button @click="cart_item.decrementQuantity(); refreshValues()" class="product_button" id="decrement_btn">-</button>
<button class="product_button" id="quantity_btn">{{cart_item.getQuantity()}}<span>x</span></button>
<button @click="cart_item.incrementQuantity(); refreshValues()" class="product_button" id="increment_btn">+</button>
<button @click="deleteItemByIndex(index)" id="delete_item_btn"><img src="../../assets/img/Icons/Delete_Icon.png"></button>
<p class="cart_text" id="cart_product_total_price">{{toEuro(cart_item.getTotalPrice())}} €</p>
</tr>
</table>
</div>
</div>
</div>
</template>
<script>
export default {
name: 'shopping-cart',
props:{
cart_items: null,
totalQuantity: 0,
totalPrice: 0.00,
},
methods: {
getImage: function(path) {
return require('../../assets/img/Products/' + path);
},
toEuro: function(value) {
if (typeof value !== "number") {
return value;
}
return value.toFixed(2);
},
deleteCart(){
this.$emit('cart-deleted');
},
deleteItemByIndex(index){
this.$emit('item-deleted', index);
},
refreshValues(){
this.$emit('values-updated');
},
},
}
</script>
(父)-Startview:
<template>
<shopping-cart
@cart-deleted="deleteCart"
@item-deleted="deleteItem"
@values-updated="refreshValues"
:cart_items="cart_items"
:totalQuantity="totalQuantity"
:totalPrice="totalPrice">
</shopping-cart>
</template>
import ShoppingCart from "@/components/container_components/shopping-cart.vue"
import {ShoppingCartService} from "@/services/shoppingcart.service"
let cart_service = new ShoppingCartService();
@Options({
components: {
ShoppingCart,
},
data() {
return{
totalQuantity: 0,
totalPrice: 0.00,
cart_items: [],
}
},
methods: {
productAdded(product: Product){
this.cart_items = cart_service.addProduct(this.cart_items, product);
this.refreshValues();
},
calcTotalPrice(){
this.totalPrice = cart_service.calcTotalPrice(this.cart_items);
},
calcTotalQuantity(){
this.totalQuantity = cart_service.calcTotalQuantity(this.cart_items);
},
deleteItem(index: number){
this.cart_items = cart_service.deleteItem(this.cart_items, index);
this.refreshValues();
},
deleteCart(){
this.cart_items = [];
this.totalPrice = 0.00;
this.totalQuantity = 0;
},
refreshValues(){
this.calcTotalQuantity();
this.calcTotalPrice();
},
【问题讨论】:
-
能否提供代码?
-
当然 - 等等..
-
全球商店通常是这种情况,因为购物车逻辑是应用程序范围的,而不仅仅适用于购物车页面。
标签: typescript vue.js