angular入门篇3----购物车添加

Angular入门3

1. 购物车组件

我们先把项目的内容丰富一下。

1.1 创建购物车功能

Store/src/app/model下,创建:

cart.model.ts

import { Injectable } from"@angular/core";
import { Product } from "./product.model";

@Injectable()
export class Cart{
    public lines: CartLine[]= [];
    public itemCount : number = 0;
    public cartPrice : number = 0;

    addLine(product:Product, quantity: number=1){
        let line=this.lines.find(line => line.product.id == product.id);

        if(line!= undefined) {
            line.quantity += quantity;
        }else{
            this.lines.push(new CartLine(product, quantity));
        }
        this.recalculate();
    }

    updateQuantity(product: Product, quantityEventTarget: EventTarget|null) {
        let quantity;
        if(quantityEventTarget==null) quantity=0;
        else{
            let quantityObject =quantityEventTarget as HTMLInputElement;
            quantity= quantityObject.value;
        }
        let line = this.lines.find(line => line.product.id == product.id);
        if(line != undefined) {
            line.quantity =Number(quantity);
        }
        this.recalculate();
    }

    removeLine(id:number) {
        let index = this.lines.findIndex(line => line.product.id == id);
        //总金额减少
        let line = this.lines.find(line => line.product.id == id) ?? null;
        if(line != null)
          this.cartPrice -= line.quantity*line.product.price;

        this.lines.splice(index, 1);
    }

    clear() {
        this.lines = []
        this.itemCount = 0;
        this.cartPrice = 0;
    }

    private recalculate() {
        this.itemCount = 0;
        this.cartPrice = 0;
        this.lines.forEach(l =>{
            this.itemCount += l.quantity;
            this.cartPrice += (l.quantity*l.product.price)
        })
    }

}

export class CartLine {
    constructor(public product: Product, public quantity: number) { }

    get lineTotal() {
        return this.quantity*this.product.price;
    }
}

在model.module.ts中完成注册:

1.2 在store文件注册购物车

在Store/src/app/store中添加购物车

在Store/src/app/store创建:

cartSummary.component.ts

import { Component } from "@angular/core";
import { Cart } from "../model/cart.model";

@Component({
    selector: "cart-summary",
    moduleId: module.id,
    templateUrl: "cartSummary.component.html"
})

export class CartSummaryComponent{
    constructor(public cart: Cart) { }
}

cartSummary.component.html

<div class="pull-xs-right">
    <small>
        Your Cart:
        <span *ngIf="cart.itemCount > 0">
            {{ cart.itemCount }} item(s)
            {{ cart.cartPrice | currency: "USD" : true: "2.2-2"}}
        </span>
        <span *ngIf="cart.itemCount == 0">
            (empty)
        </span>
    </small>
    <button class="btn btn-sm bg-inverse" [disabled]="cart.itemCount == 0" routerLink="/cart">
        <i class="fa fa-shopping-cart" aria-hidden="true" ></i> 
    </button>
</div>

在store.module.ts中注册Cart组件:

1.3 把购物小车集成到我们的商店页面

将Cart导入store.component.ts:



然后将组件放进商店的HTML模板:

1.4 运行项目 && 效果

此时我们的项目结构:

2. 额外内容补充

在 .module.ts文件内,我们对一些关键词做一下解释:
providers:将该模块所有在组件中注入,比如将Cart注入到model。

declaration:声明一些模块中要使用到的一些组件,指令,管道等。比如我们自定义了一些在HTML模板中使用的指令(directive),类似于ngFor、ngIf等,我们便需要在此声明(可参考《Angular5高级编程》第七章7.5.4自定义的counter指令代替ngFor)。

exports:导出组件or指令管道等,供外部使用,非常经典了。

bootstrap:这个模块启动的时候应该启动的组件。

schemas:不属于Angular的组件或者指令的元素或者属性都需要在这里进行声明。

3. 参考博客

Angular NgModule(模块) 作者:huangenai

posted @ 2022-09-22 11:07  Feo2022  阅读(135)  评论(0)    收藏  举报