import { AppComponent } from './app.component';
import { TopBarComponent } from './top-bar/top-bar.component';
import { ProductListComponent } from './product-list/product-list.component';
+import { ProductDetailsComponent } from './product-details/product-details.component';
@NgModule({
declarations: [
AppComponent,
TopBarComponent,
- ProductListComponent
+ ProductListComponent,
+ ProductDetailsComponent
],
imports: [
BrowserModule,
RouterModule.forRoot([
- { path: '', component: ProductListComponent }
+ { path: '', component: ProductListComponent },
+ { path: 'products/:productId', component: ProductDetailsComponent }
])
],
providers: [],
--- /dev/null
+<h2>Product Details</h2>
+
+<div *ngIf="product">
+ <h3>{{ product.name }}</h3>
+ <h4>{{ product.price | currency }}</h4>
+ <p>{{ product.description }}</p>
+</div>
--- /dev/null
+import { async, ComponentFixture, TestBed } from '@angular/core/testing';
+
+import { ProductDetailsComponent } from './product-details.component';
+
+describe('ProductDetailsComponent', () => {
+ let component: ProductDetailsComponent;
+ let fixture: ComponentFixture<ProductDetailsComponent>;
+
+ beforeEach(async(() => {
+ TestBed.configureTestingModule({
+ declarations: [ ProductDetailsComponent ]
+ })
+ .compileComponents();
+ }));
+
+ beforeEach(() => {
+ fixture = TestBed.createComponent(ProductDetailsComponent);
+ component = fixture.componentInstance;
+ fixture.detectChanges();
+ });
+
+ it('should create', () => {
+ expect(component).toBeTruthy();
+ });
+});
--- /dev/null
+import { Component, OnInit } from '@angular/core';
+import { ActivatedRoute } from '@angular/router';
+import { products } from '../products';
+
+@Component({
+ selector: 'app-product-details',
+ templateUrl: './product-details.component.html',
+ styleUrls: ['./product-details.component.css']
+})
+export class ProductDetailsComponent implements OnInit {
+ product;
+
+ constructor(private route: ActivatedRoute) { }
+
+ ngOnInit() {
+ this.route.paramMap.subscribe(params => {
+ this.product = products[+params.get('productId')];
+ });
+ }
+
+}