1

When I trying go to myaddress/home I get the error. When a user enter in address line myaddress/home need to redirect him to myaddress/home/allQuestions. Another routes work. I use angular 8.

    const routes: Routes = [
      {
        path: '', component: MainLayoutComponent, children: [
          {path: '', redirectTo: '/login', pathMatch: 'full'},
          {path: 'login', component: LoginFormComponent},
          {path: 'registration', component: RegistrationFormComponent}
        ]
      },
      { path: 'home', component: HomeLayoutComponent, children: [
          {path: '', redirectTo: '/allQuestions', pathMatch: 'full'},
          {path: 'allQuestions', component: AllQuestionsComponent},
          {path: 'addQuestion', component: AddQuestionComponent},
        ], canActivate: [AuthenticationGuard] 
      }
    ];

Error:

ERROR Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'allQuestions'
Error: Cannot match any routes. URL Segment: 'allQuestions'

1 Answer 1

0

Right now with your code , angular router , will try to replace the hole route address (/home) with /login.

To set address to redirectTo, You can not use /

change the second line

...
{ path: 'home', component: HomeLayoutComponent, children: [
          {path: '', redirectTo: '/allQuestions', pathMatch: 'full'},
...

to this

{path: '', redirectTo: 'allQuestions', pathMatch: 'full'},

Bug fixed code

const routes: Routes = [
      {
        path: '', component: MainLayoutComponent, children: [
          {path: '', redirectTo: 'login', pathMatch: 'full'},
          {path: 'login', component: LoginFormComponent},
          {path: 'registration', component: RegistrationFormComponent}
        ]
      },
      { path: 'home', component: HomeLayoutComponent, children: [
          {path: '', redirectTo: 'allQuestions', pathMatch: 'full'},
          {path: 'allQuestions', component: AllQuestionsComponent},
          {path: 'addQuestion', component: AddQuestionComponent},
        ], canActivate: [AuthenticationGuard] 
      }
    ];

Next Step

After you fixed this code , make sure change the way you load your components and consider about using lazy loading.

What i mean is , for example for authorization purposes you can have AuthModule, that module can have his own auth-router.module.ts, that contain route information for auth module (like /signin and /signup ). This way modules will load only if the route that goes there called !

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.