How to navigate to other page in angular 6?

前端 未结 4 442
粉色の甜心
粉色の甜心 2021-02-04 04:38

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\';         


        
4条回答
  •  北恋
    北恋 (楼主)
    2021-02-04 05:38

    @sasi.. try like this,

     
    

    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

    1. .forRoot(appRoutes) for app level component registration like featuteModules(ex. UserManagement) and components which you want register at root level.
    2. .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

    routerLink directive gives you absolute path match like navigateByUrl() of Router class.

     
    

    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.

    • If the first segment begins with /, the router will look up the route from the root of the app.
    • If the first segment begins with ./, or doesn't begin with a slash, the router will instead look in the children of the current activated route.
    • And if the first segment begins with ../, the router will go up one level.

    for more info have look here.. routerLink

    Router class

    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.

    1. navigate() :

      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).

    2. navigateByUrl()

      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.

提交回复
热议问题