Skip to content

Commit e1fa8cb

Browse files
authored
docs(angular): document standalone routing usage (#3167)
1 parent 5996a81 commit e1fa8cb

File tree

1 file changed

+98
-6
lines changed

1 file changed

+98
-6
lines changed

docs/angular/build-options.md

Lines changed: 98 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ All Ionic imports should be imported from the `@ionic/angular/standalone` submod
3636

3737
Ionic Angular needs to be configured when the Angular application calls `bootstrapApplication` using the `provideIonicAngular` function. Developers can pass any [IonicConfig](../developing/config#ionicconfig) values as an object in this function. Note that `provideIonicAngular` needs to be called even if no custom config is passed.
3838

39-
```typescript
39+
```typescript title="main.ts"
4040
import { enableProdMode, importProvidersFrom } from '@angular/core';
4141
import { bootstrapApplication } from '@angular/platform-browser';
4242
import { RouteReuseStrategy, provideRouter } from '@angular/router';
@@ -63,7 +63,7 @@ bootstrapApplication(AppComponent, {
6363

6464
In the example below, we are importing `IonContent` and `IonButton` from `@ionic/angular/standalone` and passing them to `imports` for use in the component template. We would get a compiler error if these components were not imported and provided to the `imports` array.
6565

66-
```typescript
66+
```typescript title="home.page.ts"
6767
import { Component } from '@angular/core';
6868
import { IonButton, IonContent } from '@ionic/angular/standalone';
6969

@@ -87,7 +87,7 @@ We recommend calling `addIcons` in the Angular component `constructor` so the da
8787

8888
For developers using Ionicons 7.2 or newer, passing only the SVG data will cause the string name to be automatically generated.
8989

90-
```typescript
90+
```typescript title="home.page.ts"
9191
import { Component } from '@angular/core';
9292
import { IonIcon } from '@ionic/angular/standalone';
9393
import { addIcons } from 'ionicons';
@@ -113,6 +113,33 @@ export class HomePage {
113113
}
114114
```
115115

116+
**Routing**
117+
118+
Developers who wish to use `routerLink`, `routerAction`, or `routerDirection` on Ionic components should import the `IonRouterLink` directive. Developers who wish to use these routing features on anchor (`<a>`) elements should import `IonRouterLinkWithHref` instead.
119+
120+
```typescript title="home.page.ts"
121+
import { Component } from '@angular/core';
122+
import { RouterLink } from '@angular/router';
123+
import { IonButton, IonRouterLink } from '@ionic/angular/standalone';
124+
125+
@Component({
126+
selector: 'app-home',
127+
templateUrl: 'home.page.html',
128+
styleUrls: ['home.page.scss'],
129+
standalone: true,
130+
imports: [
131+
IonButton,
132+
RouterLink, // required to get base routerLink behavior for @angular/router
133+
IonRouterLink, // use IonRouterLinkWithHref if you are using an <a> element instead
134+
],
135+
})
136+
export class HomePage {}
137+
```
138+
139+
```html title="home.page.html"
140+
<ion-button routerLink="/foo" routerDirection="root">Go to Foo Page</ion-button>
141+
```
142+
116143
### Usage with NgModule-based Applications
117144

118145
:::caution
@@ -123,7 +150,7 @@ All Ionic imports should be imported from the `@ionic/angular/standalone` submod
123150

124151
Ionic Angular needs to be configured in the `providers` array of `app.module.ts` using the `provideIonicAngular` function. Developers can pass any [IonicConfig](../developing/config#ionicconfig) values as an object in this function. Note that `provideIonicAngular` needs to be called even if no custom config is passed.
125152

126-
```typescript
153+
```typescript title="app.module.ts"
127154
import { NgModule } from '@angular/core';
128155
import { BrowserModule } from '@angular/platform-browser';
129156
import { RouteReuseStrategy } from '@angular/router';
@@ -146,7 +173,7 @@ export class AppModule {}
146173

147174
In the example below, we are importing `IonContent` and `IonButton` from `@ionic/angular/standalone` and passing them to `imports` array in the Angular component's NgModule for use in the component template. We would get a compiler error if these components were not imported and provided to the `imports` array.
148175

149-
```typescript
176+
```typescript title="home.module.ts"
150177
import { NgModule } from '@angular/core';
151178
import { IonButton, IonContent } from '@ionic/angular/standalone';
152179
import { HomePage } from './home.page';
@@ -168,7 +195,7 @@ We recommend calling `addIcons` in the Angular component `constructor` so the da
168195

169196
For developers using Ionicons 7.2 or newer, passing only the SVG data will cause the string name to be automatically generated.
170197

171-
```typescript
198+
```typescript title="home.page.ts"
172199
import { Component } from '@angular/core';
173200
import { addIcons } from 'ionicons';
174201
import { logoIonic } from 'ionicons/icons';
@@ -191,6 +218,34 @@ export class HomePage {
191218
}
192219
```
193220

221+
**Routing**
222+
223+
Developers who wish to use `routerLink`, `routerAction`, or `routerDirection` on Ionic components should import the `IonRouterLink` directive. Developers who wish to use these routing features on anchor (`<a>`) elements should import `IonRouterLinkWithHref` instead.
224+
225+
```typescript title="home.module.ts"
226+
import { NgModule } from '@angular/core';
227+
import { RouterLink } from '@angular/router';
228+
import { IonButton, IonRouterLink } from '@ionic/angular/standalone';
229+
import { HomePage } from './home.page';
230+
231+
import { HomePageRoutingModule } from './home-routing.module';
232+
233+
@NgModule({
234+
imports: [
235+
IonButton,
236+
RouterLink, // required to get base routerLink behavior for @angular/router
237+
IonRouterLink, // use IonRouterLinkWithHref if you are using an <a> element instead
238+
HomePageRoutingModule,
239+
],
240+
declarations: [HomePage],
241+
})
242+
export class HomePageModule {}
243+
```
244+
245+
```html title="home.page.html"
246+
<ion-button routerLink="/foo" routerDirection="root">Go to Foo Page</ion-button>
247+
```
248+
194249
## Modules
195250

196251
### Overview
@@ -332,6 +387,26 @@ export class TestComponent {
332387
- }
333388
```
334389

390+
9. If you are using `routerLink`, `routerDirection`, or `routerAction` be sure to import the `IonRouterLink` directive for Ionic components or `IonRouterLinkWithHref` directive for `<a>` elements.
391+
392+
```diff title="test.component.ts"
393+
import { Component } from '@angular/core';
394+
- import { IonButton } from '@ionic/angular/standalone';
395+
+ import { IonButton, IonRouterLink } from '@ionic/angular/standalone';
396+
397+
@Component({
398+
selector: 'app-root',
399+
templateUrl: 'app.component.html',
400+
styleUrls: ['app.component.scss'],
401+
standalone: true,
402+
imports: [
403+
IonButton,
404+
+ IonRouterLink
405+
],
406+
})
407+
export class TestComponent {}
408+
```
409+
335410
### NgModule-based Applications
336411

337412
Follow these steps if your Angular application is still using the NgModule architecture, but you want to adopt Ionic UI components as standalone components now.
@@ -457,3 +532,20 @@ export class TestComponentModule {}
457532
- "output": "./svg"
458533
- }
459534
```
535+
536+
9. If you are using `routerLink`, `routerDirection`, or `routerAction` be sure to import the `IonRouterLink` directive for Ionic components or `IonRouterLinkWithHref` directive for `<a>` elements.
537+
538+
```diff title="test.module.ts"
539+
import { NgModule } from '@angular/core';
540+
import { TestComponent } from './test.component';
541+
- import { IonButton } from '@ionic/angular/standalone';
542+
+ import { IonButton, IonRouterLink } from '@ionic/angular/standalone';
543+
544+
@NgModule({
545+
imports: [
546+
IonButton,
547+
+ IonRouterLink,
548+
],
549+
declarations: [TestComponent]
550+
})
551+
```

0 commit comments

Comments
 (0)