9. 服务 service

2019-08-01  本文已影响0人  晨曦Bai

1. 创建购物车服务

import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class CartService {

constructor() {}
}
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class CartService {
items = [];
addToCart(product) {
    this.items.push(product);
  }

  getItems() {
    return this.items;
  }

  clearCart() {
    this.items = [];
    return this.items;
  }
}
constructor() {}
}

2. 使用购物车服务

在商品详情(product-details)视图中添加一个“Buy”按钮。单击“Buy”按钮后,你将借助购物车服务来把当前商品添加到购物车中。


》 src/app/product-details/product-details.component.ts



》src/app/product-details/product-details.component.ts


import { Componet, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { products }  from '../products';
import { CartService } from '../cart.service';

export  class  ProductDetailsComponent  implements  OnInit  {
contructor (
private route: ActivatedRoute,
private cartService: CartService
) {}

addToCart(product) {
window.alert('Your product has been added to the cart!');
this.cartService.addToCart(product);
}
}


》 src/app/product-details/product-details.component.html


<h2>Product Details</h2>
 <div *ngIf="product">
  <h3>{{ product.name }}</h3> 
 <h4>{{ product.price | currency }}</h4> 
 <p>{{ product.description }}</p> 
 <button (click)="addToCart(product)">Buy</button>  </div>

2. 创建购物车页面

  import  {  Component,  OnInit }  from  '@angular/core';
 @Component({
selector:  'app-cart',
templateUrl:  './cart.component.html',
 styleUrls:  ['./cart.component.css']
 })
  export  class  CartComponent  implements  OnInit {
  constructor()  {  }
 ngOnInit()  {
 }
 }


》 app.module.ts


@NgModule({
imports: [
BrowserModule,
RouterModule.forRoot([
{path: ' ', component: ProductListComponent},
{path: 'products/:productId', component: ProductDetailsComponent},
{ path: 'cart', component: CartComponent },
])
]
})
<a [routerLink]="['/']">
  <h1>My Store</h1>
</a>

<a [routerLink]="['/cart']" class="button fancy-button"><i class="material-icons">shopping_cart</i>Checkout</a>

》src/app/cart/cart.component.ts


import  {  Component  }  from  '@angular/core';  
import  {  CartService  }  from  '../cart.service';
export class CartComponent {
items;
constructor (
private cartService: CartService
) {
this.items=this.cartService.getItems();
}
}

》src/app/cart/cart.component.html


<h3>Cart</h3>  
<div  class="cart-item" *ngFor="let item of items">  <span>{{ item.name }}</span>  
<span>{{ item.price | currency }}</span>  
</div>

3. 检索运费价格

[
  {
    "type": "Overnight",
    "price": 25.99
  },
  {
    "type": "2-Day",
    "price": 9.99
  },
  {
    "type": "Postal",
    "price": 2.99
  }
]

》src/app/app.module.ts


import  {  NgModule}  from  '@angular/core'; 
import  {  BrowserModule}  from  '@angular/platform-browser';  
import  {  RouterModule }  from  '@angular/router';  import  {  HttpClientModule }  from  '@angular/common/http'; 
 import  {  ReactiveFormsModule }  from  '@angular/forms';  
import  {  AppComponent  }  from  './app.component';  import  {  TopBarComponent  }  from  './top-bar/top-bar.component'; 
import  {  ProductListComponent  }  from  './product-list/product-list.component';  
import  {  ProductAlertsComponent  }  from  './product-alerts/product-alerts.component';  
import  {  ProductDetailsComponent  }  from  './product-details/product-details.component';  

@NgModule({
 imports:  [ 
 BrowserModule, 
HttpClientModule, 
ReactiveFormsModule,
RouterModule.forRoot([  
{ path:  '', component:  ProductListComponent  },  
{ path:  'products/:productId', component:  ProductDetailsComponent  }, 
 { path:  'cart', component:  CartComponent  }, 
 ])  
], 
declarations:  [  
AppComponent,  
TopBarComponent, 
 ProductListComponent, 
 ProductAlertsComponent, 
 ProductDetailsComponent, 
 CartComponent,  ],
 bootstrap:  [  AppComponent  ]
  })  
export  class  AppModule  {  }


》src/app/cart.service.ts


import  {  Injectable}  from  '@angular/core';  
import  {  HttpClient  }  from  '@angular/common/http;
export  class  CartService  { 
items =  []; 
 constructor(  private  http:  HttpClient  ) 
 {}  
addToCart(product) {
    this.items.push(product);
  }

  getItems() {
    return this.items;
  }

  clearCart() {
    this.items = [];
    return this.items;
  }
getShippingPrices() {
return this.http.get('/assets/shipping.json')
}
}

上一篇下一篇

猜你喜欢

热点阅读