@ngrdt/router 0.0.17 → 0.0.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +254 -254
- package/fesm2022/ngrdt-router.mjs +222 -61
- package/fesm2022/ngrdt-router.mjs.map +1 -1
- package/index.d.ts +434 -12
- package/package.json +1 -3
- package/esm2022/index.mjs +0 -13
- package/esm2022/lib/directives/rdt-any-route-active.directive.mjs +0 -126
- package/esm2022/lib/directives/rdt-router-link.directive.mjs +0 -69
- package/esm2022/lib/guards/global-route-guard.service.mjs +0 -51
- package/esm2022/lib/guards/prevent-data-loss.guard.mjs +0 -7
- package/esm2022/lib/rdt-route/constants.mjs +0 -5
- package/esm2022/lib/rdt-route/rdt-angular-route.mjs +0 -196
- package/esm2022/lib/rdt-route/rdt-route-builder.mjs +0 -112
- package/esm2022/lib/rdt-route/rdt-route.mjs +0 -308
- package/esm2022/lib/rdt-route/utils.mjs +0 -15
- package/esm2022/lib/services/rdt-cannot-be-entered.token.mjs +0 -7
- package/esm2022/lib/services/rdt-confirm-data-loss.service.mjs +0 -11
- package/esm2022/lib/services/rdt-router.service.mjs +0 -161
- package/esm2022/lib/services/rdt-routes.token.mjs +0 -3
- package/esm2022/ngrdt-router.mjs +0 -5
- package/lib/directives/rdt-any-route-active.directive.d.ts +0 -34
- package/lib/directives/rdt-router-link.directive.d.ts +0 -23
- package/lib/guards/global-route-guard.service.d.ts +0 -10
- package/lib/guards/prevent-data-loss.guard.d.ts +0 -2
- package/lib/rdt-route/constants.d.ts +0 -3
- package/lib/rdt-route/rdt-angular-route.d.ts +0 -29
- package/lib/rdt-route/rdt-route-builder.d.ts +0 -46
- package/lib/rdt-route/rdt-route.d.ts +0 -138
- package/lib/rdt-route/utils.d.ts +0 -21
- package/lib/services/rdt-cannot-be-entered.token.d.ts +0 -7
- package/lib/services/rdt-confirm-data-loss.service.d.ts +0 -9
- package/lib/services/rdt-router.service.d.ts +0 -61
- package/lib/services/rdt-routes.token.d.ts +0 -3
package/README.md
CHANGED
|
@@ -1,255 +1,255 @@
|
|
|
1
|
-
# @ngrdt/router
|
|
2
|
-
|
|
3
|
-
Main use for this package is to define routes and their parameters in one place in app. It is recommended to define special `routes` module in every app using `@ngrdt/router` where all `RdtRoute` objects are defined using `RdtRouteBuilder`. Individual application modules then import these objects, define Angular routes and provide implementation details such as which component is rendered, state, guards, etc.
|
|
4
|
-
|
|
5
|
-
This approach ensures that each route is defined exactly once and can be easily edited if necessary. `@ngrdt/router` also provides set of useful tools to help you with common routing tasks.
|
|
6
|
-
|
|
7
|
-
### Instalation
|
|
8
|
-
|
|
9
|
-
`npm install @ngrdt/router`
|
|
10
|
-
|
|
11
|
-
## Basic usage
|
|
12
|
-
|
|
13
|
-
### Application routes file
|
|
14
|
-
|
|
15
|
-
Create special `nx` library (not `NgModule`) for instance `@example-app/routes`. In this library you will create file called `routes.ts` and in it individual `RdtRoute` objects like so:
|
|
16
|
-
|
|
17
|
-
```typescript
|
|
18
|
-
export const DASHBOARD_ITEM_ROUTE = new RdtRouteBuilder()
|
|
19
|
-
.setName('Dashboard item')
|
|
20
|
-
.setPath(':id')
|
|
21
|
-
.setParam('id', 'number')
|
|
22
|
-
.build();
|
|
23
|
-
|
|
24
|
-
export const DASHBOARD_ROUTE = new RdtRouteBuilder()
|
|
25
|
-
.setName('Dashboard')
|
|
26
|
-
.setPath('dashboard')
|
|
27
|
-
.setChildren(DASHBOARD_ITEM_ROUTE)
|
|
28
|
-
.build();
|
|
29
|
-
|
|
30
|
-
export const SETTINGS_ROUTE = new RdtRouteBuilder()
|
|
31
|
-
.setName('Settings')
|
|
32
|
-
.setPath('settings')
|
|
33
|
-
.setCanBeEntered(() =>
|
|
34
|
-
inject(ExampleAppPermissionService).hasPermission('SETTINGS_ACCESS')
|
|
35
|
-
)
|
|
36
|
-
.build();
|
|
37
|
-
```
|
|
38
|
-
|
|
39
|
-
This file should only contain imports to interfaces used by `RdtRouteBuilder` as template parameter. No component, ngModule or guards such as `CanActivateFn` should be imported here. This approach will save you later headache with circular dependency.
|
|
40
|
-
|
|
41
|
-
`RdtRouterService` needs to be aware of all routes defined therefore you must provide it in your `ApplicationConfig` like so:
|
|
42
|
-
|
|
43
|
-
```typescript
|
|
44
|
-
import { RDT_ROUTES_PROVIDER, RdtRoute } from '@ngrdt/router';
|
|
45
|
-
import * as ALL_ROUTES_OBJ from './rdt-routes';
|
|
46
|
-
const ALL_ROUTES = Object.values(ALL_ROUTES_OBJ) as RdtRoute[];
|
|
47
|
-
|
|
48
|
-
export const appConfig: ApplicationConfig = {
|
|
49
|
-
providers: [
|
|
50
|
-
{
|
|
51
|
-
provide: RDT_ROUTES_PROVIDER,
|
|
52
|
-
useValue: ALL_ROUTES,
|
|
53
|
-
},
|
|
54
|
-
]
|
|
55
|
-
}
|
|
56
|
-
```
|
|
57
|
-
|
|
58
|
-
### Angular routes files
|
|
59
|
-
|
|
60
|
-
Now you have to bind `RdtRoute` objects to individual modules, components, states, effects, guards and other implementation details. Instead of defining routes directly you import `RdtRoute` from `@example-app/routes` and then define `routes.ts` like so:
|
|
61
|
-
|
|
62
|
-
```typescript
|
|
63
|
-
export const routes: Route[] = [
|
|
64
|
-
DASHBOARD_ROUTE.toAngularRoute()
|
|
65
|
-
.setComponent(DashboardComponent)
|
|
66
|
-
.setChildren(
|
|
67
|
-
DASHBOARD_ITEM_ROUTE.toAngularRoute()
|
|
68
|
-
.setComponent(DashboardItemComponent)
|
|
69
|
-
.addCanDeactivate(preventDataLossGuardFn)
|
|
70
|
-
.build()
|
|
71
|
-
)
|
|
72
|
-
.build(),
|
|
73
|
-
]
|
|
74
|
-
```
|
|
75
|
-
|
|
76
|
-
This array is then used as in regular Angular project. Note that child routes must be added as children here as well.
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
### Child routes and parameters
|
|
80
|
-
|
|
81
|
-
You may nest child routes and also create routes with typed parameters to link them with particular type.
|
|
82
|
-
|
|
83
|
-
For instance lets say there's a route displaying users which has a child route to display more information about a single user. By providing `User` type to `RdtRouteBuilder` and defining types of parameters we can use `RdtRoute` object to parse path to extract parameters.
|
|
84
|
-
|
|
85
|
-
```typescript
|
|
86
|
-
interface User {
|
|
87
|
-
userId: number;
|
|
88
|
-
name: string;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
export const USER_DETAIL_ROUTE = new RdtRouteBuilder<User>()
|
|
92
|
-
.setName('User detail')
|
|
93
|
-
.setPath(':userId/:name')
|
|
94
|
-
.setParam('userId', 'number')
|
|
95
|
-
.setParam('name', 'string')
|
|
96
|
-
.build();
|
|
97
|
-
|
|
98
|
-
export const USER_LIST_ROUTE = new RdtRouteBuilder()
|
|
99
|
-
.setName('Users')
|
|
100
|
-
.setPath('users')
|
|
101
|
-
.setChildren(USER_DETAIL_ROUTE)
|
|
102
|
-
.build();
|
|
103
|
-
```
|
|
104
|
-
|
|
105
|
-
Then we navigate to user detail like so:
|
|
106
|
-
|
|
107
|
-
```typescript
|
|
108
|
-
const params: Partial<User> = {
|
|
109
|
-
userId: 123,
|
|
110
|
-
name: 'Josef',
|
|
111
|
-
};
|
|
112
|
-
|
|
113
|
-
inject(RdtRouterService).navigate(USER_DETAIL_ROUTE, params);
|
|
114
|
-
```
|
|
115
|
-
|
|
116
|
-
`RdtRouterService` is aware of the type so you can use it to parse absolute path into `User` object. Route knows `userId` is `number` so it will parse it. If parameter is missing or is of wrong type, then `parseAbsoluteUrl` returns `null`. Return value is a wrapper object with `get(route: RdtRoute)` method to get parsed parameters of given route or its parents.
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
```typescript
|
|
120
|
-
const user: User = USER_DETAIL_ROUTE
|
|
121
|
-
.parse('/users/123/Josef')
|
|
122
|
-
.get(USER_DETAIL_ROUTE);
|
|
123
|
-
```
|
|
124
|
-
|
|
125
|
-
### More parameter examples
|
|
126
|
-
|
|
127
|
-
#### Static parameters
|
|
128
|
-
|
|
129
|
-
Imagine your app contains list of users and each user has set of tasks. The routes are defined as follows:
|
|
130
|
-
|
|
131
|
-
```typescript
|
|
132
|
-
interface User {
|
|
133
|
-
userId: number,
|
|
134
|
-
name: string,
|
|
135
|
-
address: string,
|
|
136
|
-
tasks: Task[]
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
interface Task {
|
|
140
|
-
taskId: number,
|
|
141
|
-
description: string
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
export const TASK_DETAIL_ROUTE = new RdtRouteBuilder<Task>()
|
|
146
|
-
.setName('Task detail')
|
|
147
|
-
.setPath('task/:taskId')
|
|
148
|
-
.setParam('taskId', 'number')
|
|
149
|
-
.build();
|
|
150
|
-
|
|
151
|
-
export const USER_DETAIL_ROUTE = new RdtRouteBuilder<User>()
|
|
152
|
-
.setName('User detail')
|
|
153
|
-
.setPath(':userId')
|
|
154
|
-
.setParam('userId', 'number')
|
|
155
|
-
.setChildren(TASK_DETAIL_ROUTE)
|
|
156
|
-
.build();
|
|
157
|
-
|
|
158
|
-
export const USER_LIST_ROUTE = new RdtRouteBuilder()
|
|
159
|
-
.setName('Users')
|
|
160
|
-
.setPath('user')
|
|
161
|
-
.setChildren(USER_DETAIL_ROUTE)
|
|
162
|
-
.build();
|
|
163
|
-
```
|
|
164
|
-
|
|
165
|
-
This translates into path `/user/:userId/task/:taskId` for task detail route.
|
|
166
|
-
|
|
167
|
-
Now inside `UserDetailComponent` there's a for loop rendering individual tasks and you want to have links to them. You cannot use directly:
|
|
168
|
-
|
|
169
|
-
```html
|
|
170
|
-
<button [rdtRouterLink]="TASK_DETAIL_ROUTE" [params]="task"></button>
|
|
171
|
-
```
|
|
172
|
-
Because task does not specify `userId`. For this you must provide static parameters for every parent route - `User` for `USER_DETAIL_ROUTE` in this case:
|
|
173
|
-
|
|
174
|
-
```typescript
|
|
175
|
-
@Input()
|
|
176
|
-
user!: User;
|
|
177
|
-
|
|
178
|
-
taskRoute!: RdtRoute<Task>;
|
|
179
|
-
|
|
180
|
-
ngOnInit() {
|
|
181
|
-
this.taskRoute = TASK_DETAIL_ROUTE.withStaticParams(USER_DETAIL_ROUTE, this.user);
|
|
182
|
-
}
|
|
183
|
-
```
|
|
184
|
-
Method `withStaticParams()` returns clone of `TASK_DETAIL_ROUTE` and all of its parent routes while fixing parameters. You can chain calling `withStaticParams` with itself or other method calls.
|
|
185
|
-
|
|
186
|
-
```html
|
|
187
|
-
<button [rdtRouterLink]="taskRoute" [params]="task"></button>
|
|
188
|
-
```
|
|
189
|
-
|
|
190
|
-
## Ecosystem
|
|
191
|
-
|
|
192
|
-
### RdtRouterLink
|
|
193
|
-
|
|
194
|
-
`RdtRouterLink` is equivalent of `RouterLink` in Angular. It supports basically the same functionality, but adds type safety. `RdtRouterLink` will be disabled if `canBeEntered` of its route returns `false`.
|
|
195
|
-
|
|
196
|
-
```typescript
|
|
197
|
-
readonly userRoute = USER_DETAIL_ROUTE;
|
|
198
|
-
readonly user$ = inject(UserService).getUser$();
|
|
199
|
-
```
|
|
200
|
-
|
|
201
|
-
```html
|
|
202
|
-
<button
|
|
203
|
-
[rdtRouterLink]="userRoute"
|
|
204
|
-
[params]="user$ | async"
|
|
205
|
-
target="_blank">
|
|
206
|
-
Link to user
|
|
207
|
-
</button>
|
|
208
|
-
```
|
|
209
|
-
|
|
210
|
-
### RdtMenu
|
|
211
|
-
|
|
212
|
-
`RdtRoute` can be passed directly into `RdtMenuItem`. If `canBeEntered` returns `false`, then the item won't be visible. This is useful to hide items user has no permission for.
|
|
213
|
-
|
|
214
|
-
Method `withStaticParams` lets us provide parameters for given `RdtRoute`, because `RdtMenuItem` does not accept route parameters.
|
|
215
|
-
|
|
216
|
-
```typescript
|
|
217
|
-
public getMenuItems(): RdtMenuItem[] {
|
|
218
|
-
const user: Partial<User> = {
|
|
219
|
-
userId: 123,
|
|
220
|
-
name: 'Josef'
|
|
221
|
-
};
|
|
222
|
-
|
|
223
|
-
return [
|
|
224
|
-
{
|
|
225
|
-
label: 'All users',
|
|
226
|
-
routerLink: USER_LIST_ROUTE
|
|
227
|
-
},
|
|
228
|
-
{
|
|
229
|
-
label: 'Josef',
|
|
230
|
-
routerLink: USER_DETAIL_ROUTE.withStaticParams(user)
|
|
231
|
-
}
|
|
232
|
-
];
|
|
233
|
-
}
|
|
234
|
-
```
|
|
235
|
-
|
|
236
|
-
### RdtAngularRoute
|
|
237
|
-
|
|
238
|
-
`RdtAngularRoute` is a container class for `RdtRoute` which functionally does two things: verifies that routes were defined correctly and enables you to provide guards, effects, component or module. Don't forget to call `build()` for it to generate Angular `Route`.
|
|
239
|
-
|
|
240
|
-
### RdtRouterService
|
|
241
|
-
|
|
242
|
-
`RdtRouterService` has similar features as Angular `Router`.
|
|
243
|
-
|
|
244
|
-
#### `parseAbsoluteUrl()`
|
|
245
|
-
Method will parse absolute path, recognize `RdtRoute` and parse its parameters. If no parameter is provided, then window location is used.
|
|
246
|
-
|
|
247
|
-
#### `navigateBack()`
|
|
248
|
-
|
|
249
|
-
Will push parent route to navigation stack. Function works anywhere in the app unlike similar Angular-based solution using `ActivatedRoute`.
|
|
250
|
-
|
|
251
|
-
Hitting native browser back button will take you back forward. In case you want true back function, use `inject(Location).back()` which removes from navigation stack, but can potentially take you outside of the app or close browser window.
|
|
252
|
-
|
|
253
|
-
#### `previousUrl` and `currentUrl`
|
|
254
|
-
|
|
1
|
+
# @ngrdt/router
|
|
2
|
+
|
|
3
|
+
Main use for this package is to define routes and their parameters in one place in app. It is recommended to define special `routes` module in every app using `@ngrdt/router` where all `RdtRoute` objects are defined using `RdtRouteBuilder`. Individual application modules then import these objects, define Angular routes and provide implementation details such as which component is rendered, state, guards, etc.
|
|
4
|
+
|
|
5
|
+
This approach ensures that each route is defined exactly once and can be easily edited if necessary. `@ngrdt/router` also provides set of useful tools to help you with common routing tasks.
|
|
6
|
+
|
|
7
|
+
### Instalation
|
|
8
|
+
|
|
9
|
+
`npm install @ngrdt/router`
|
|
10
|
+
|
|
11
|
+
## Basic usage
|
|
12
|
+
|
|
13
|
+
### Application routes file
|
|
14
|
+
|
|
15
|
+
Create special `nx` library (not `NgModule`) for instance `@example-app/routes`. In this library you will create file called `routes.ts` and in it individual `RdtRoute` objects like so:
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
export const DASHBOARD_ITEM_ROUTE = new RdtRouteBuilder()
|
|
19
|
+
.setName('Dashboard item')
|
|
20
|
+
.setPath(':id')
|
|
21
|
+
.setParam('id', 'number')
|
|
22
|
+
.build();
|
|
23
|
+
|
|
24
|
+
export const DASHBOARD_ROUTE = new RdtRouteBuilder()
|
|
25
|
+
.setName('Dashboard')
|
|
26
|
+
.setPath('dashboard')
|
|
27
|
+
.setChildren(DASHBOARD_ITEM_ROUTE)
|
|
28
|
+
.build();
|
|
29
|
+
|
|
30
|
+
export const SETTINGS_ROUTE = new RdtRouteBuilder()
|
|
31
|
+
.setName('Settings')
|
|
32
|
+
.setPath('settings')
|
|
33
|
+
.setCanBeEntered(() =>
|
|
34
|
+
inject(ExampleAppPermissionService).hasPermission('SETTINGS_ACCESS')
|
|
35
|
+
)
|
|
36
|
+
.build();
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
This file should only contain imports to interfaces used by `RdtRouteBuilder` as template parameter. No component, ngModule or guards such as `CanActivateFn` should be imported here. This approach will save you later headache with circular dependency.
|
|
40
|
+
|
|
41
|
+
`RdtRouterService` needs to be aware of all routes defined therefore you must provide it in your `ApplicationConfig` like so:
|
|
42
|
+
|
|
43
|
+
```typescript
|
|
44
|
+
import { RDT_ROUTES_PROVIDER, RdtRoute } from '@ngrdt/router';
|
|
45
|
+
import * as ALL_ROUTES_OBJ from './rdt-routes';
|
|
46
|
+
const ALL_ROUTES = Object.values(ALL_ROUTES_OBJ) as RdtRoute[];
|
|
47
|
+
|
|
48
|
+
export const appConfig: ApplicationConfig = {
|
|
49
|
+
providers: [
|
|
50
|
+
{
|
|
51
|
+
provide: RDT_ROUTES_PROVIDER,
|
|
52
|
+
useValue: ALL_ROUTES,
|
|
53
|
+
},
|
|
54
|
+
]
|
|
55
|
+
}
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### Angular routes files
|
|
59
|
+
|
|
60
|
+
Now you have to bind `RdtRoute` objects to individual modules, components, states, effects, guards and other implementation details. Instead of defining routes directly you import `RdtRoute` from `@example-app/routes` and then define `routes.ts` like so:
|
|
61
|
+
|
|
62
|
+
```typescript
|
|
63
|
+
export const routes: Route[] = [
|
|
64
|
+
DASHBOARD_ROUTE.toAngularRoute()
|
|
65
|
+
.setComponent(DashboardComponent)
|
|
66
|
+
.setChildren(
|
|
67
|
+
DASHBOARD_ITEM_ROUTE.toAngularRoute()
|
|
68
|
+
.setComponent(DashboardItemComponent)
|
|
69
|
+
.addCanDeactivate(preventDataLossGuardFn)
|
|
70
|
+
.build()
|
|
71
|
+
)
|
|
72
|
+
.build(),
|
|
73
|
+
]
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
This array is then used as in regular Angular project. Note that child routes must be added as children here as well.
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
### Child routes and parameters
|
|
80
|
+
|
|
81
|
+
You may nest child routes and also create routes with typed parameters to link them with particular type.
|
|
82
|
+
|
|
83
|
+
For instance lets say there's a route displaying users which has a child route to display more information about a single user. By providing `User` type to `RdtRouteBuilder` and defining types of parameters we can use `RdtRoute` object to parse path to extract parameters.
|
|
84
|
+
|
|
85
|
+
```typescript
|
|
86
|
+
interface User {
|
|
87
|
+
userId: number;
|
|
88
|
+
name: string;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export const USER_DETAIL_ROUTE = new RdtRouteBuilder<User>()
|
|
92
|
+
.setName('User detail')
|
|
93
|
+
.setPath(':userId/:name')
|
|
94
|
+
.setParam('userId', 'number')
|
|
95
|
+
.setParam('name', 'string')
|
|
96
|
+
.build();
|
|
97
|
+
|
|
98
|
+
export const USER_LIST_ROUTE = new RdtRouteBuilder()
|
|
99
|
+
.setName('Users')
|
|
100
|
+
.setPath('users')
|
|
101
|
+
.setChildren(USER_DETAIL_ROUTE)
|
|
102
|
+
.build();
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
Then we navigate to user detail like so:
|
|
106
|
+
|
|
107
|
+
```typescript
|
|
108
|
+
const params: Partial<User> = {
|
|
109
|
+
userId: 123,
|
|
110
|
+
name: 'Josef',
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
inject(RdtRouterService).navigate(USER_DETAIL_ROUTE, params);
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
`RdtRouterService` is aware of the type so you can use it to parse absolute path into `User` object. Route knows `userId` is `number` so it will parse it. If parameter is missing or is of wrong type, then `parseAbsoluteUrl` returns `null`. Return value is a wrapper object with `get(route: RdtRoute)` method to get parsed parameters of given route or its parents.
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
```typescript
|
|
120
|
+
const user: User = USER_DETAIL_ROUTE
|
|
121
|
+
.parse('/users/123/Josef')
|
|
122
|
+
.get(USER_DETAIL_ROUTE);
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
### More parameter examples
|
|
126
|
+
|
|
127
|
+
#### Static parameters
|
|
128
|
+
|
|
129
|
+
Imagine your app contains list of users and each user has set of tasks. The routes are defined as follows:
|
|
130
|
+
|
|
131
|
+
```typescript
|
|
132
|
+
interface User {
|
|
133
|
+
userId: number,
|
|
134
|
+
name: string,
|
|
135
|
+
address: string,
|
|
136
|
+
tasks: Task[]
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
interface Task {
|
|
140
|
+
taskId: number,
|
|
141
|
+
description: string
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
export const TASK_DETAIL_ROUTE = new RdtRouteBuilder<Task>()
|
|
146
|
+
.setName('Task detail')
|
|
147
|
+
.setPath('task/:taskId')
|
|
148
|
+
.setParam('taskId', 'number')
|
|
149
|
+
.build();
|
|
150
|
+
|
|
151
|
+
export const USER_DETAIL_ROUTE = new RdtRouteBuilder<User>()
|
|
152
|
+
.setName('User detail')
|
|
153
|
+
.setPath(':userId')
|
|
154
|
+
.setParam('userId', 'number')
|
|
155
|
+
.setChildren(TASK_DETAIL_ROUTE)
|
|
156
|
+
.build();
|
|
157
|
+
|
|
158
|
+
export const USER_LIST_ROUTE = new RdtRouteBuilder()
|
|
159
|
+
.setName('Users')
|
|
160
|
+
.setPath('user')
|
|
161
|
+
.setChildren(USER_DETAIL_ROUTE)
|
|
162
|
+
.build();
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
This translates into path `/user/:userId/task/:taskId` for task detail route.
|
|
166
|
+
|
|
167
|
+
Now inside `UserDetailComponent` there's a for loop rendering individual tasks and you want to have links to them. You cannot use directly:
|
|
168
|
+
|
|
169
|
+
```html
|
|
170
|
+
<button [rdtRouterLink]="TASK_DETAIL_ROUTE" [params]="task"></button>
|
|
171
|
+
```
|
|
172
|
+
Because task does not specify `userId`. For this you must provide static parameters for every parent route - `User` for `USER_DETAIL_ROUTE` in this case:
|
|
173
|
+
|
|
174
|
+
```typescript
|
|
175
|
+
@Input()
|
|
176
|
+
user!: User;
|
|
177
|
+
|
|
178
|
+
taskRoute!: RdtRoute<Task>;
|
|
179
|
+
|
|
180
|
+
ngOnInit() {
|
|
181
|
+
this.taskRoute = TASK_DETAIL_ROUTE.withStaticParams(USER_DETAIL_ROUTE, this.user);
|
|
182
|
+
}
|
|
183
|
+
```
|
|
184
|
+
Method `withStaticParams()` returns clone of `TASK_DETAIL_ROUTE` and all of its parent routes while fixing parameters. You can chain calling `withStaticParams` with itself or other method calls.
|
|
185
|
+
|
|
186
|
+
```html
|
|
187
|
+
<button [rdtRouterLink]="taskRoute" [params]="task"></button>
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
## Ecosystem
|
|
191
|
+
|
|
192
|
+
### RdtRouterLink
|
|
193
|
+
|
|
194
|
+
`RdtRouterLink` is equivalent of `RouterLink` in Angular. It supports basically the same functionality, but adds type safety. `RdtRouterLink` will be disabled if `canBeEntered` of its route returns `false`.
|
|
195
|
+
|
|
196
|
+
```typescript
|
|
197
|
+
readonly userRoute = USER_DETAIL_ROUTE;
|
|
198
|
+
readonly user$ = inject(UserService).getUser$();
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
```html
|
|
202
|
+
<button
|
|
203
|
+
[rdtRouterLink]="userRoute"
|
|
204
|
+
[params]="user$ | async"
|
|
205
|
+
target="_blank">
|
|
206
|
+
Link to user
|
|
207
|
+
</button>
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
### RdtMenu
|
|
211
|
+
|
|
212
|
+
`RdtRoute` can be passed directly into `RdtMenuItem`. If `canBeEntered` returns `false`, then the item won't be visible. This is useful to hide items user has no permission for.
|
|
213
|
+
|
|
214
|
+
Method `withStaticParams` lets us provide parameters for given `RdtRoute`, because `RdtMenuItem` does not accept route parameters.
|
|
215
|
+
|
|
216
|
+
```typescript
|
|
217
|
+
public getMenuItems(): RdtMenuItem[] {
|
|
218
|
+
const user: Partial<User> = {
|
|
219
|
+
userId: 123,
|
|
220
|
+
name: 'Josef'
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
return [
|
|
224
|
+
{
|
|
225
|
+
label: 'All users',
|
|
226
|
+
routerLink: USER_LIST_ROUTE
|
|
227
|
+
},
|
|
228
|
+
{
|
|
229
|
+
label: 'Josef',
|
|
230
|
+
routerLink: USER_DETAIL_ROUTE.withStaticParams(user)
|
|
231
|
+
}
|
|
232
|
+
];
|
|
233
|
+
}
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
### RdtAngularRoute
|
|
237
|
+
|
|
238
|
+
`RdtAngularRoute` is a container class for `RdtRoute` which functionally does two things: verifies that routes were defined correctly and enables you to provide guards, effects, component or module. Don't forget to call `build()` for it to generate Angular `Route`.
|
|
239
|
+
|
|
240
|
+
### RdtRouterService
|
|
241
|
+
|
|
242
|
+
`RdtRouterService` has similar features as Angular `Router`.
|
|
243
|
+
|
|
244
|
+
#### `parseAbsoluteUrl()`
|
|
245
|
+
Method will parse absolute path, recognize `RdtRoute` and parse its parameters. If no parameter is provided, then window location is used.
|
|
246
|
+
|
|
247
|
+
#### `navigateBack()`
|
|
248
|
+
|
|
249
|
+
Will push parent route to navigation stack. Function works anywhere in the app unlike similar Angular-based solution using `ActivatedRoute`.
|
|
250
|
+
|
|
251
|
+
Hitting native browser back button will take you back forward. In case you want true back function, use `inject(Location).back()` which removes from navigation stack, but can potentially take you outside of the app or close browser window.
|
|
252
|
+
|
|
253
|
+
#### `previousUrl` and `currentUrl`
|
|
254
|
+
|
|
255
255
|
Cached sync values of previous and current path values. For example `/dashboard` or `/user/123/task/xyz`.
|