| Sep | OCT | Nov |
| 28 | ||
| 2020 | 2021 | 2022 |
COLLECTED BY
Collection: Common Crawl
import { RouterModule } from '@angular/router';
@NgModule({
imports: [
...
RouterModule.forRoot([
{ path: '', component: LoginComponent },
{ path: 'detail', component: DetailComponent },
])
],
}) '', which is essentially our index route. So for this, we load the
LoginComponent. Fairly straight forward. This pattern of matching paths with a component continues for every entry we have in the router config. But what if we wanted to load a different path on our initial load?
[
{ path: '', redirectTo: 'login', pathMatch: 'full' },
{ path: 'login', component: LoginComponent },
{ path: 'detail', component: DetailComponent }
];
login route. The last key of pathMatch is required to tell the router how it should look up the path.
Since we use full, we're telling the router that we should compare the full path, even if ends up being something like
/route1/route2/route3. Meaning that if we have:
{ path: '/route1/route2/route3', redirectTo: 'login', pathMatch: 'full' },
{ path: 'login', component: LoginComponent }, /route1/route2/route3 we'll redirect. But if we loaded
/route1/route2/route4, we won't redirect, as the paths don't match fully.
Alternatively, if we used:
{ path: '/route1/route2', redirectTo: 'login', pathMatch: 'prefix' },
{ path: 'login', component: LoginComponent },/route1/route2/route3 and
/route1/route2/route4, we'll be redirected for both routes. This is because
pathMatch: 'prefix' will match only part of the path.
routerLink directive. Let's go back and take our simple router setup from earlier:
RouterModule.forRoot([
{ path: '', component: LoginComponent },
{ path: 'detail', component: DetailComponent }
]);
LoginComponent, we can use the following HTML to navigate to the detail route.
<ion-header>
<ion-toolbar>
<ion-title>Login</ion-title>
</ion-toolbar>
</ion-header>
<ion-content class="ion-padding">
<ion-button [routerLink]="['/detail']">Go to detail</ion-button>
</ion-content>ion-button and
routerLink directive. RouterLink works on a similar idea as typical
hrefs, but instead of building out the URL as a string, it can be built as an array, which can provide more complicated paths.
We also can programmatically navigate in our app by using the router API.
import { Component } from '@angular/core';
import { Router } from '@angular/router';
@Component({
...
})
export class LoginComponent {
constructor(private router: Router){}
navigate(){
this.router.navigate(['/detail'])
}
}
import { RouterModule } from '@angular/router';
@NgModule({
imports: [
...
RouterModule.forRoot([
{ path: '', redirectTo: 'login', pathMatch: 'full' },
{ path: 'login', loadChildren: () => import('./login/login.module').then(m =>m.LoginModule) },
{ path: 'detail', loadChildren: () => import('./detail/detail.module').then(m =>m.DetailModule) }
])
],
}) loadChildren property is a way to reference a module by using native import instead of a component directly. In order to do this though, we need to create a module for each of the components.
...
import { RouterModule } from '@angular/router';
import { LoginComponent } from './login.component';
@NgModule({
imports: [
...
RouterModule.forChild([
{ path: '', component: LoginComponent },
])
],
})
forChild and declaring the component in that setup. With this setup, when we run our build, we will produce separate chunks for both the app component, the login component, and the detail component.
const routes: Routes = [
{
path: 'tabs',
component: TabsPage,
children: [
{
path: 'tab1',
children: [
{
path: '',
loadChildren: () => import('../tab1/tab1.module').then(m =>m.Tab1PageModule)
}
]
},
{
path: '',
redirectTo: '/tabs/tab1',
pathMatch: 'full'
}
]
},
{
path: '',
redirectTo: '/tabs/tab1',
pathMatch: 'full'
}
];
<ion-tabs>
<ion-tab-bar slot="bottom">
<ion-tab-button tab="tab1">
<ion-icon name="flash"></ion-icon>
<ion-label>Tab One</ion-label>
</ion-tab-button>
</ion-tab-bar>
</ion-tabs> ion-tabs component, and provide a
ion-tab-bar. The
ion-tab-bar provides a
ion-tab-button with a
tab property that is associated with the tab "outlet" in the router config. Note that the latest version of
@ionic/angular no longer requires <ion-tab>, but instead allows developers to fully customize the tab bar, and the single source of truth lives within the router configuration.
Previous
Lifecycle
Next
Config