import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
+import { HttpClientModule } from '@angular/common/http';
import { AppComponent } from './app.component';
import { TopBarComponent } from './top-bar/top-bar.component';
import { ProductDetailsComponent } from './product-details/product-details.component';
import { ProductAlertsComponent } from './product-alerts/product-alerts.component';
import { CartComponent } from './cart/cart.component';
+import { ShippingComponent } from './shipping/shipping.component';
@NgModule({
declarations: [
ProductListComponent,
ProductDetailsComponent,
ProductAlertsComponent,
- CartComponent
+ CartComponent,
+ ShippingComponent
],
imports: [
BrowserModule,
+ HttpClientModule,
RouterModule.forRoot([
{ path: '', component: ProductListComponent },
{ path: 'products/:productId', component: ProductDetailsComponent },
- { path: 'cart', component: CartComponent }
+ { path: 'cart', component: CartComponent },
+ { path: 'shipping', component: ShippingComponent }
])
],
providers: [],
import { Injectable } from '@angular/core';
+import { HttpClient } from '@angular/common/http';
@Injectable({
providedIn: 'root'
items= [];
- constructor() { }
+ constructor(private http: HttpClient) { }
addToCart(product) {
this.items.push(product);
this.items = [];
return this.items;
}
+
+ getShippingPrices() {
+ return this.http.get('/assets/shipping.json');
+ }
}
<h3>Cart</h3>
+<p>
+ <a routerLink="/shipping">Shipping Prices</a>
+</p>
+
<div class="cart-item" *ngFor="let item of items">
<span>{{ item.name }}</span>
<span>{{ item.price | currency }}</span>
--- /dev/null
+<h3>Shipping Prices</h3>
+
+<div class="shipping-item" *ngFor="let shipping of shippingCosts | async">
+ <span>{{ shipping.type }}</span>
+ <span>{{ shipping.price | currency }}</span>
+</div>
--- /dev/null
+import { async, ComponentFixture, TestBed } from '@angular/core/testing';
+
+import { ShippingComponent } from './shipping.component';
+
+describe('ShippingComponent', () => {
+ let component: ShippingComponent;
+ let fixture: ComponentFixture<ShippingComponent>;
+
+ beforeEach(async(() => {
+ TestBed.configureTestingModule({
+ declarations: [ ShippingComponent ]
+ })
+ .compileComponents();
+ }));
+
+ beforeEach(() => {
+ fixture = TestBed.createComponent(ShippingComponent);
+ component = fixture.componentInstance;
+ fixture.detectChanges();
+ });
+
+ it('should create', () => {
+ expect(component).toBeTruthy();
+ });
+});
--- /dev/null
+import { Component, OnInit } from '@angular/core';
+import { CartService } from '../cart.service';
+
+@Component({
+ selector: 'app-shipping',
+ templateUrl: './shipping.component.html',
+ styleUrls: ['./shipping.component.css']
+})
+export class ShippingComponent implements OnInit {
+
+ shippingCosts;
+
+ constructor(private cartService: CartService) { }
+
+ ngOnInit() {
+ this.shippingCosts = this.cartService.getShippingPrices();
+ }
+
+}
--- /dev/null
+[
+ {
+ "type": "Overnight",
+ "price": 25.99
+ },
+ {
+ "type": "2-Day",
+ "price": 9.99
+ },
+ {
+ "type": "Postal",
+ "price": 2.99
+ }
+]
\ No newline at end of file