Skip to content

Commit 1d61de8

Browse files
authored
Angular standalone sample updates (#7547)
This PR: - Removes unused `CommonModule` - Uses `styleUrl` instead of `styleUrls` - Uses `inject` for DI - Uses `takeUntilDestroyed` instead of `Subject/OnDestroy` approach - Fixes indents There is a typing issue, which is now solved by a generic in the following line: ```ts private msalGuardConfig = inject<MsalGuardConfiguration>(MSAL_GUARD_CONFIG); ``` I found a fix in the next PR: #7484 After its merge, the generic can be removed: ```ts private msalGuardConfig = inject(MSAL_GUARD_CONFIG); ```
1 parent 6679938 commit 1d61de8

File tree

1 file changed

+22
-31
lines changed

1 file changed

+22
-31
lines changed

samples/msal-angular-samples/angular-standalone-sample/src/app/app.component.ts

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Component, OnInit, Inject, OnDestroy } from '@angular/core';
2-
import { CommonModule } from '@angular/common';
1+
import { Component, DestroyRef, inject, OnInit } from '@angular/core';
2+
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
33
import { MatMenuModule } from '@angular/material/menu';
44
import { MatButtonModule } from '@angular/material/button';
55
import { MatToolbarModule } from '@angular/material/toolbar';
@@ -19,38 +19,34 @@ import {
1919
EventMessage,
2020
EventType,
2121
} from '@azure/msal-browser';
22-
import { Subject } from 'rxjs';
23-
import { filter, takeUntil } from 'rxjs/operators';
22+
import { filter } from 'rxjs/operators';
2423

2524
@Component({
26-
selector: 'app-root',
27-
templateUrl: './app.component.html',
28-
styleUrls: ['./app.component.css'],
29-
imports: [
30-
CommonModule,
31-
MsalModule,
32-
RouterOutlet,
33-
RouterLink,
34-
MatToolbarModule,
35-
MatButtonModule,
36-
MatMenuModule,
37-
]
25+
selector: 'app-root',
26+
templateUrl: './app.component.html',
27+
styleUrl: './app.component.css',
28+
imports: [
29+
MsalModule,
30+
RouterOutlet,
31+
RouterLink,
32+
MatToolbarModule,
33+
MatButtonModule,
34+
MatMenuModule,
35+
]
3836
})
39-
export class AppComponent implements OnInit, OnDestroy {
37+
export class AppComponent implements OnInit {
38+
private destroyRef = inject(DestroyRef);
39+
private msalGuardConfig = inject<MsalGuardConfiguration>(MSAL_GUARD_CONFIG);
40+
private authService = inject(MsalService);
41+
private msalBroadcastService = inject(MsalBroadcastService);
42+
4043
title = 'Angular Standalone Sample - MSAL Angular';
4144
isIframe = false;
4245
loginDisplay = false;
43-
private readonly _destroying$ = new Subject<void>();
44-
45-
constructor(
46-
@Inject(MSAL_GUARD_CONFIG) private msalGuardConfig: MsalGuardConfiguration,
47-
private authService: MsalService,
48-
private msalBroadcastService: MsalBroadcastService
49-
) {}
5046

5147
ngOnInit(): void {
5248
this.authService.handleRedirectObservable().subscribe();
53-
49+
5450
this.isIframe = window !== window.parent && !window.opener; // Remove this line to use Angular Universal
5551

5652
this.msalBroadcastService.msalSubject$
@@ -74,7 +70,7 @@ export class AppComponent implements OnInit, OnDestroy {
7470
filter(
7571
(status: InteractionStatus) => status === InteractionStatus.None
7672
),
77-
takeUntil(this._destroying$)
73+
takeUntilDestroyed(this.destroyRef)
7874
)
7975
.subscribe(() => {
8076
this.setLoginDisplay();
@@ -138,9 +134,4 @@ export class AppComponent implements OnInit, OnDestroy {
138134
this.authService.logoutRedirect();
139135
}
140136
}
141-
142-
ngOnDestroy(): void {
143-
this._destroying$.next(undefined);
144-
this._destroying$.complete();
145-
}
146137
}

0 commit comments

Comments
 (0)