Im trying to redirect my page from login to another page. Im following this code.
My login component ts file:
import { Router } from '@angular/router';
constructor(private router: Router) {
}
funLogin(mobilenumber){
this.router.navigateByUrl('registration');
}
In my html Im calling this function in a submit btn,
<button class="common-btn btn" (click)="funLogin(mobileNo.value)">Submit</button>
In my app.login.routing file,
export const loginRouting: Routes = [
{
path: '', component: DashboardRootComponent, canActivateChild: [],
children: [
{ path: '', component: DashboardComponent, pathMatch: 'full' },
{ path: 'home', component: HomeComponent },
{ path: 'registration', component: RegistrationComponent },
]
}
]
I have tried with "this.router.navigate" & referredlot of links. But it didnt work. Can anyone please tell me where Im going wrong or if you could give me a workingh sample it would be better.
@sasi.. try like this,
<a routerLink="/registration"><button class="btn btn-success" > Submit </button></a>
Update :
In order to use the routing in your application, you must register the components which allows the angular router to render the view.
We need register our components in App Module
or any Feature Module
of it (your current working module) in order to route to specific component view.
We can register components in two ways
.forRoot(appRoutes)
for app level component registration like
featuteModules
(ex. UserManagement) and components
which you want register at root level
..forChild(featureRoutes)
for feature modules child components
(Ex. UserDelete, UserUpdate).
you can register something like below,
const appRoutes: Routes = [
{ path: 'user', loadChildren: './user/user.module#UserModule' },
{ path: 'heroes', component: HeroListComponent },
];
@NgModule({
imports: [
BrowserModule,
FormsModule,
RouterModule.forRoot(
appRoutes
)
],
P.S : In order to navigate from one component to another, you must include the
RouterModule
in corresponding Module Imports array from@angular/router
package.
You can navigate one to another page in Angular in Two ways. (both are same at wrapper level but the implementation from our side bit diff so.)
routerLink
directive gives you absolute path match like navigateByUrl()
of Router
class.
<a [routerLink]=['/registration']><button class="btn btn-success" > Submit </button></a>
If you use dynamic values
to generate the link, you can pass an array
of path segments, followed by the params
for each segment.
For instance routerLink=['/team', teamId, 'user', userName, {details: true}]
means that we want to generate a link to /team/11/user/bob;details=true
.
There are some useful points to be remembered when we are using routerLink
.
/
, the router will look up the route
from the root of the app../
, or doesn't begin with a slash,
the router will instead look in the children of the current activated
route.../
, the router will go up one
level.for more info have look here.. routerLink
We need inject Router
class into the component in order to use it's methods.
There more than two methods to navigate like navigate()
, navigateByUrl()
, and some other.. but we will mostly use these two.
Navigate based on the provided array of commands and a starting point. If no starting route is provided, the navigation is absolute.
this.route.navigate(['/team/113/user/ganesh']);
navigate()
command will append the latest string is append to existing URL. We can also parse the queryParams
from this method like below,
this.router.navigate(['/team/'], {
queryParams: { userId: this.userId, userName: this.userName }
});
You can get the these values with ActivatedRoute
in navigated Component. you can check here more about paramMap
, snapshot(no-observable alternative)
.
Navigate based on the provided URL, which must be absolute.
this.route.navigateByUrl(['/team/113/user/ganesh']);
navigateByUrl()
is similar to changing the location bar directly–we are providing the whole new URL.