Skip to content

Commit 782dc6f

Browse files
Routing
1 parent 1517592 commit 782dc6f

File tree

6 files changed

+35
-13
lines changed

6 files changed

+35
-13
lines changed

src/app/app.component.html

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
1-
<h1>cart : {{cart.length}}</h1>
2-
<app-home></app-home>
1+
<h1>cart : {{cart.length}}
2+
<!-- <app-home></app-home> -->
3+
<button class="btn btn-outline-primary" routerLink="/">Home</button>
4+
<button class="btn btn-outline-primary" routerLink="/cart">Cart</button>
5+
6+
<router-outlet></router-outlet>

src/app/app.module.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,14 @@ import { AppComponent } from './app.component';
66
import { CartComponent } from './cart/cart.component';
77
import { ShortenPipe } from './shorten.pipe';
88
import { HomeComponent } from './home/home.component';
9+
import { RouterModule, Routes } from '@angular/router';
910

11+
12+
const routes:Routes = [
13+
{path:'',component:HomeComponent},
14+
{path:'cart',component:CartComponent},
15+
16+
]
1017
@NgModule({
1118
declarations: [
1219
AppComponent,
@@ -16,7 +23,8 @@ import { HomeComponent } from './home/home.component';
1623
],
1724
imports: [
1825
BrowserModule,
19-
FormsModule
26+
FormsModule,
27+
RouterModule.forRoot(routes)
2028
],
2129
providers: [],
2230
bootstrap: [AppComponent]

src/app/cart/cart.component.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,17 @@ export class CartComponent implements OnInit, OnChanges {
2424
},2000)
2525

2626
this.cart = this.dService.cart; //reference binding
27+
this.customer = this.dService.name
2728
}
2829

2930
ngOnInit(): void {
30-
console.log('ngOnInit')
31+
console.log(this.constructor.name,'ngOnInit')
3132
}
3233
ngOnChanges(): void {
33-
console.log('ngOnChanges')
34+
console.log(this.constructor.name,'ngOnChanges')
3435
}
3536
ngOnDestroy(): void {
36-
console.log('ngOnDestroy')
37+
console.log(this.constructor.name,'ngOnDestroy')
3738
}
3839
// getItem(e:any){
3940
// this.item= e.target.value;

src/app/data.service.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Injectable } from '@angular/core';
55
})
66
export class DataService {
77
cart = [];
8+
name='dummyName';
89
constructor() {
910
console.log(this.constructor.name)
1011
}

src/app/home/home.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ <h3> Total Items via Service: {{cart.length}}</h3>
77

88
<br>
99

10-
<app-cart [customer]="name" (cartEvent)="updateCart($event)"></app-cart>
10+
<!-- <app-cart [customer]="name" (cartEvent)="updateCart($event)"></app-cart> -->

src/app/home/home.component.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { Component, OnInit } from '@angular/core';
1+
import { Component, OnChanges, OnDestroy, OnInit } from '@angular/core';
22
import { DataService } from '../data.service';
33

44
@Component({
55
selector: 'app-home',
66
templateUrl: './home.component.html',
77
styleUrls: ['./home.component.css']
88
})
9-
export class HomeComponent implements OnInit {
9+
export class HomeComponent implements OnInit,OnChanges,OnDestroy {
1010
name=''
1111
items=[];
1212
cart =[];
@@ -16,13 +16,21 @@ export class HomeComponent implements OnInit {
1616

1717
}
1818

19-
ngOnInit(): void {
19+
ngOnInit(): void {
20+
console.log(this.constructor.name,'ngOnInit')
21+
}
22+
ngOnChanges(): void {
23+
console.log(this.constructor.name,'ngOnChanges')
24+
}
25+
ngOnDestroy(): void {
26+
console.log(this.constructor.name,'ngOnDestroy')
2027
}
2128

2229
enterName(n:any){
23-
this.name = n.value;
24-
n.value = ''
25-
console.log(n.value)
30+
// this.name = n.value;
31+
// n.value = ''
32+
// console.log(n.value)
33+
this.dService.name = n.value;
2634
}
2735

2836
updateCart(e:any){

0 commit comments

Comments
 (0)