Skip to content

Commit 44b29cf

Browse files
refactor: update to new Angular v19 features (#1603)
1 parent dadcfb0 commit 44b29cf

13 files changed

+53
-58
lines changed

apps/analog-app/src/app/app.component.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ import { TopBarComponent } from '@analogjs/top-bar';
88
imports: [TopBarComponent, RouterOutlet],
99
template: `
1010
<analogjs-top-bar />
11-
1211
<div class="container">
13-
<router-outlet></router-outlet>
12+
<router-outlet />
1413
</div>
1514
`,
1615
})

apps/analog-app/src/app/cart.service.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Injectable } from '@angular/core';
1+
import { inject, Injectable } from '@angular/core';
22
import { HttpClient } from '@angular/common/http';
33
import type { Product } from './products';
44

@@ -8,7 +8,7 @@ import type { Product } from './products';
88
export class CartService {
99
items: Product[] = [];
1010

11-
constructor(private http: HttpClient) {}
11+
private readonly http = inject(HttpClient);
1212

1313
addToCart(product: Product) {
1414
this.items.push(product);

apps/analog-app/src/app/pages/(home).page.ts

+9-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1+
import { RouteMeta, injectLoad } from '@analogjs/router';
12
import { Component } from '@angular/core';
23
import { toSignal } from '@angular/core/rxjs-interop';
3-
import { RouteMeta, injectLoad } from '@analogjs/router';
4-
import { NgForOf, NgIf } from '@angular/common';
54
import { RouterLinkWithHref } from '@angular/router';
65

76
import { ProductAlertsComponent } from '../product-alerts/product-alerts.component';
@@ -13,12 +12,12 @@ export const routeMeta: RouteMeta = {
1312

1413
@Component({
1514
selector: 'app-product-list',
16-
standalone: true,
17-
imports: [NgForOf, NgIf, ProductAlertsComponent, RouterLinkWithHref],
15+
imports: [ProductAlertsComponent, RouterLinkWithHref],
1816
template: `
1917
<h2>Products</h2>
2018
21-
<div *ngFor="let product of data().products">
19+
@for (product of data().products; track product.id) {
20+
<div>
2221
<h3>
2322
<a
2423
[title]="product.name + ' details'"
@@ -27,14 +26,13 @@ export const routeMeta: RouteMeta = {
2726
{{ product.name }}
2827
</a>
2928
</h3>
30-
31-
<p *ngIf="product.description">Description: {{ product.description }}</p>
32-
29+
@if ( product.description ) {
30+
<p>Description: {{ product.description }}</p>
31+
}
3332
<button type="button" (click)="share()">Share</button>
34-
35-
<app-product-alerts [product]="product" (notify)="onNotify()">
36-
</app-product-alerts>
33+
<app-product-alerts [product]="product" (notify)="onNotify()" />
3734
</div>
35+
}
3836
`,
3937
styles: [
4038
`

apps/analog-app/src/app/pages/[...slug].page.ts

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { Component } from '@angular/core';
22

33
@Component({
44
selector: 'app-page-not-found',
5-
standalone: true,
65
template: ` <h2>Page Not Found</h2> `,
76
})
87
export default class PageNotFoundComponent {}

apps/analog-app/src/app/pages/cart.page.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { CurrencyPipe, NgForOf } from '@angular/common';
1+
import { CurrencyPipe } from '@angular/common';
22
import { Component, inject } from '@angular/core';
33
import { FormBuilder, ReactiveFormsModule } from '@angular/forms';
44
import { RouterLinkWithHref } from '@angular/router';
@@ -7,19 +7,21 @@ import { CartService } from '../cart.service';
77

88
@Component({
99
selector: 'app-cart',
10-
standalone: true,
11-
imports: [RouterLinkWithHref, NgForOf, CurrencyPipe, ReactiveFormsModule],
10+
imports: [RouterLinkWithHref, CurrencyPipe, ReactiveFormsModule],
1211
template: `
1312
<h3>Cart</h3>
1413
1514
<p>
1615
<a routerLink="/shipping">Shipping Prices</a>
1716
</p>
1817
19-
<div class="cart-item" *ngFor="let item of items">
18+
@for (item of items; track $index) {
19+
20+
<div class="cart-item">
2021
<span>{{ item.name }} </span>
2122
<span>{{ item.price | currency }}</span>
2223
</div>
24+
}
2325
2426
<form [formGroup]="checkoutForm" (ngSubmit)="onSubmit()">
2527
<div>
@@ -37,8 +39,8 @@ import { CartService } from '../cart.service';
3739
`,
3840
})
3941
export default class CartComponent {
40-
private cartService = inject(CartService);
41-
private formBuilder = inject(FormBuilder);
42+
private readonly cartService = inject(CartService);
43+
private readonly formBuilder = inject(FormBuilder);
4244

4345
items = this.cartService.getItems();
4446

apps/analog-app/src/app/pages/client/(client).page.ts

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
22
import { ServerOnly } from '@analogjs/router';
33

44
@Component({
5-
standalone: true,
65
imports: [ServerOnly],
76
changeDetection: ChangeDetectionStrategy.OnPush,
87
template: `

apps/analog-app/src/app/pages/products.[productId].page.ts

+11-9
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,33 @@
1-
import { Component, inject, OnInit } from '@angular/core';
2-
import { CurrencyPipe, NgIf } from '@angular/common';
31
import { injectActivatedRoute } from '@analogjs/router';
2+
import { CurrencyPipe } from '@angular/common';
43
import { HttpClient } from '@angular/common/http';
4+
import { Component, inject, OnInit } from '@angular/core';
55
import { catchError, of } from 'rxjs';
66

7-
import { Product } from '../products';
87
import { CartService } from '../cart.service';
8+
import { Product } from '../products';
99

1010
@Component({
1111
selector: 'app-product-details',
12-
standalone: true,
13-
imports: [NgIf, CurrencyPipe],
12+
imports: [CurrencyPipe],
1413
template: `
1514
<h2>Product Details</h2>
1615
17-
<div *ngIf="product">
16+
@if ( product ) {
17+
18+
<div>
1819
<h3>{{ product.name }}</h3>
1920
<h4>{{ product.price | currency }}</h4>
2021
<p>{{ product.description }}</p>
2122
<button type="button" (click)="addToCart(product)">Buy</button>
2223
</div>
24+
}
2325
`,
2426
})
2527
export default class ProductDetailsComponent implements OnInit {
26-
private route = injectActivatedRoute();
27-
private cartService = inject(CartService);
28-
private http = inject(HttpClient);
28+
private readonly route = injectActivatedRoute();
29+
private readonly cartService = inject(CartService);
30+
private readonly http = inject(HttpClient);
2931

3032
product: Product | undefined;
3133

apps/analog-app/src/app/pages/search.page.ts

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import type { load } from './search.server';
66

77
@Component({
88
selector: 'app-search-page',
9-
standalone: true,
109
imports: [FormAction],
1110
template: `
1211
<h3>Search</h3>

apps/analog-app/src/app/pages/shipping/[...slug].page.ts

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { Component } from '@angular/core';
22

33
@Component({
44
selector: 'app-shipping-page-not-found',
5-
standalone: true,
65
template: ` <h2>Shipping Page Not Found</h2> `,
76
})
87
export default class ShippingPageNotFoundComponent {}
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,16 @@
1-
import { AsyncPipe, CurrencyPipe, NgForOf } from '@angular/common';
2-
import { Component, inject, OnInit } from '@angular/core';
1+
import { AsyncPipe, CurrencyPipe } from '@angular/common';
2+
import { Component, inject } from '@angular/core';
33

4-
import { Observable } from 'rxjs';
54
import { CartService } from '../../cart.service';
65

76
@Component({
87
selector: 'app-shipping',
9-
standalone: true,
10-
imports: [NgForOf, CurrencyPipe, AsyncPipe],
8+
imports: [CurrencyPipe, AsyncPipe],
119
templateUrl: './shipping.html',
1210
styleUrls: ['./shipping.scss'],
1311
})
14-
export default class ShippingComponent implements OnInit {
15-
private cartService = inject(CartService);
12+
export default class ShippingComponent {
13+
private readonly cartService = inject(CartService);
1614

17-
shippingCosts!: Observable<{ type: string; price: number }[]>;
18-
19-
ngOnInit(): void {
20-
this.shippingCosts = this.cartService.getShippingPrices();
21-
}
15+
shippingCosts = this.cartService.getShippingPrices();
2216
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<h3>Shipping Prices</h3>
2-
3-
<div class="shipping-item" *ngFor="let shipping of shippingCosts | async">
2+
@for (shipping of shippingCosts | async; track $index) {
3+
<div class="shipping-item">
44
<span>{{ shipping.type }}</span>
55
<span>{{ shipping.price | currency }}</span>
66
</div>
7+
}
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
import { NgIf } from '@angular/common';
2-
import { Component, Input, Output, EventEmitter } from '@angular/core';
1+
import { Component, input, output } from '@angular/core';
32
import { Product } from '../products';
43

54
@Component({
65
selector: 'app-product-alerts',
7-
standalone: true,
8-
imports: [NgIf],
96
template: `
10-
<p *ngIf="product && product.price > 700">
7+
@if ( product() && product()!.price > 700 ) {
8+
<p>
119
<button type="button" (click)="notify.emit()">Notify Me</button>
1210
</p>
11+
}
1312
`,
1413
})
1514
export class ProductAlertsComponent {
16-
@Input() product: Product | undefined;
17-
@Output() notify = new EventEmitter();
15+
readonly product = input.required<Product>();
16+
notify = output();
1817
}

apps/analog-app/src/server/components/hello.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { ChangeDetectionStrategy, Component, computed } from '@angular/core';
1+
import {
2+
ChangeDetectionStrategy,
3+
Component,
4+
computed,
5+
OnInit,
6+
} from '@angular/core';
27

38
import {
49
injectStaticOutputs,
@@ -7,7 +12,6 @@ import {
712

813
@Component({
914
selector: 'app-hello',
10-
standalone: true,
1115
changeDetection: ChangeDetectionStrategy.OnPush,
1216
template: `
1317
<h3>Hello From the Server</h3>
@@ -22,7 +26,7 @@ import {
2226
}
2327
`,
2428
})
25-
export default class HelloComponent {
29+
export default class HelloComponent implements OnInit {
2630
Date = Date;
2731
props = injectStaticProps();
2832
outputs = injectStaticOutputs<{ loaded: boolean }>();

0 commit comments

Comments
 (0)