Skip to content

Commit d7d9526

Browse files
committed
Update state management
1 parent 1fc6b88 commit d7d9526

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

docs/src/fr/services/README.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ Lorsque Angular découvre qu'un composant dépend d'un service, il vérifie d'ab
5757

5858
Les dépendances peuvent être fournies à trois niveaux :
5959
- **au niveau root:** c'est le comportement par défaut lors de la création d'un service avec le CLI. C'est ce que signifie `providedIn: 'root'`. La *même instance* de la dépendance est injectée partout où elle est nécessaire comme s'il s'agissait d'un singleton.
60-
- **au niveau du module:** la dépendance est ajoutée au tableau de providers du `NgModule`. Le module obtient sa propre instance de la dépendance
60+
- **au niveau de la route:** la dépendance est ajoutée au tableau de providers de la `Route`. La route obtient sa propre instance de la dépendance
6161
- **au niveau du composant:** la dépendance est ajoutée au tableau des providers du composant. Chaque instance de ce composant obtient sa propre instance de la dépendance.
6262

6363
## TP : Gestion de l'État
@@ -84,17 +84,26 @@ Les dépendances peuvent être fournies à trois niveaux :
8484
</CodeGroupItem>
8585
</CodeGroup>
8686

87-
5. Afficher conditionnellement le bouton Logout en fonction du statut `loggedIn` de l'utilisateur
87+
5. Afficher conditionnellement le bouton Logout en fonction du statut `loggedIn` de l'utilisateur. Utilisez un getter dans le fichier `app.component.ts` pour passer l'information du service au template (c'est une bonne pratique que de toujours déclaré un service comme private dans la classe d'un composant).
8888
6. Utilisez un navigation guard pour rediriger l'utilisateur qui souhaite accéder à la page de recherche de films vers `/login` s'il n'est pas authentifié (rendez le CanActivate vrai si la route est accessible sinon retournez un `UrlTree` via la méthode `createUrlTree` du service `Router`). Pour prendre en considération des cas d'usage futur, ajoutez un returnUrl en tant que queryParam au `UrlTree` renvoyé afin que le `LoginFormComponent` sache où revenir après l'authentification et modifiez le `LoginFormComponent` en conséquence. Pour générer le navigation guard, utilisez la commande du CLI suivante :
8989

9090
```sh
9191
ng generate guard guards/authentication
9292
# ? Which interfaces would you like to implement? CanActivate
9393
```
9494

95+
::: details Aide pour injecter un service dans la fonction guard
96+
```ts
97+
export const authenticationGuard: CanActivateFn = (route, state) => {
98+
const authenticationService = inject(AuthenticationService)
99+
// ...
100+
}
101+
```
102+
:::
103+
95104
::: details Aide pour l'UrlTree
96105
```ts
97-
this.router.createUrlTree(['/login'], { queryParams: { returnUrl: state.url }})
106+
inject(Router).createUrlTree(['/login'], { queryParams: { returnUrl: state.url }})
98107
```
99108
:::
100109

docs/src/services/README.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ When Angular discovers that a component depends on a service, it first checks if
5757

5858
Dependencies can be provided at three levels:
5959
- **root level:** this is the default behaviour when creating a service with the CLI. That is what `providedIn: 'root'` means. The *same instance* of the dependency is injected everywhere it is needed as if it were a singleton.
60-
- **module level:** the dependency is added to the providers array of the `NgModule`. The module gets its own instance of the dependency
60+
- **route level:** the dependency is added to the providers array of the `Route`. The route gets its own instance of the dependency
6161
- **component level:** the dependency is added to the providers array of the component. Each instance of that component gets its own instance of the dependency.
6262

6363
## Practical Work: State management
@@ -84,17 +84,26 @@ Dependencies can be provided at three levels:
8484
</CodeGroupItem>
8585
</CodeGroup>
8686

87-
5. Conditionally show the Logout button depending on the `loggedIn` status of the user
87+
5. Conditionally show the Logout button depending on the `loggedIn` status of the user. Use a getter in the `app.component.ts` file to pass data from the service to the template (it is good practive to always declare a service as private in the component class).
8888
6. Use a navigation guard to redirect the user who wants to access the film search page to `/login` if they are not authenticated (make the CanActivate return true if the route can be accessed else return a `UrlTree` via the `createUrlTree` method of the `Router` service). To future-proof the guard, add a returnUrl as a queryParam to the returned `UrlTree` so that the `LoginFormComponent` knows where to navigate back to after authentication and modify the `LoginFormComponent` accordingly. To generate the navigation guard use the following CLI command:
8989

9090
```sh
9191
ng generate guard guards/authentication
9292
# ? Which interfaces would you like to implement? CanActivate
9393
```
9494

95+
::: details Help for injecting services into the guard function
96+
```ts
97+
export const authenticationGuard: CanActivateFn = (route, state) => {
98+
const authenticationService = inject(AuthenticationService)
99+
// ...
100+
}
101+
```
102+
:::
103+
95104
::: details Help for the UrlTree
96105
```ts
97-
this.router.createUrlTree(['/login'], { queryParams: { returnUrl: state.url }})
106+
inject(Router).createUrlTree(['/login'], { queryParams: { returnUrl: state.url }})
98107
```
99108
:::
100109

0 commit comments

Comments
 (0)