2

When user entry home or tab-navigation app must go on the login page.

const appRoutes: Routes = [
  {
    path: 'tab-navigation',
    redirectTo: 'login',
    pathMatch: 'full'
  },
  {
    path: 'home',
    redirectTo: 'login',
    pathMatch: 'full'
  },
  {
    path: '',
    redirectTo: 'login',
    pathMatch: 'full'
  },
  {
    path: '**',
    redirectTo: 'error404',
  }
];

This logic work when entry is '' or something like this 'dfd45657d'. Why not work for home and tab-navigation?

8
  • 1
    try to remove pathMatch: 'full' for tab-navigation and home Commented Jan 28, 2019 at 13:20
  • @Ben Thie That's what I tried first. Not work. Commented Jan 28, 2019 at 13:25
  • 1
    also, you don't have any path route for 'login', did you had something like: path: 'login', component: LoginComponent Commented Jan 28, 2019 at 13:32
  • You need a component associated with your login route Commented Jan 28, 2019 at 13:36
  • Try using remove pathmatch from tab-navigation and home Commented Jan 28, 2019 at 13:39

3 Answers 3

2

First of all, you need to create associate component for login.

Second, matchpath doesn't affect you here. so whether you keep it or not, no effect.

Try this:

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { LoginComponent } from '../login/login.component';

const routes: Routes = [
  {
    path: 'tab-navigation',
    redirectTo: 'login'
  },
  {
    path: 'home',
    redirectTo: 'login'
  },
  {
    path: '',
    redirectTo: 'login',
    pathMatch: 'full'
  },
  {
    path : 'login',
    component : LoginComponent
  },
  {
    path: '**',
    redirectTo: 'error404',
  }
];
@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})

export class RouteModule { }

Working link: https://stackblitz.com/edit/angular-nbajga?file=src%2Fapp%2Froute%2Froute.module.ts

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

Comments

1

if you had a login url in your routes, my guess will be that since login match '**' it may have something to do with two succesive redirection.

but if your really did not add a login url, you may need to add one...

const appRoutes: Routes = [
  {
    path: 'tab-navigation',
    redirectTo: 'login',
    pathMatch: 'full'
  },
  {
    path: 'home',
    redirectTo: 'login',
    pathMatch: 'full'
  },
  {
    path: '',
    redirectTo: 'login',
    pathMatch: 'full'
  },
 {
    path: 'login',
    component: 'loginComponent',
  },
  {
    path: '**',
    redirectTo: 'error404',
  }
];

2 Comments

Not work Even when I leave { path: 'tab-navigation', redirectTo: 'login', pathMatch: 'full' }, The result is the same.
do you have a login url declared ?
0

try this

const appRoutes: Routes = [
  {
    path: 'tab-navigation',
    redirectTo: '',
    pathMatch: 'full'
  },
  {
    path: 'home',
    redirectTo: '',
    pathMatch: 'full'
  },
  {
    path: '',
    redirectTo: 'login',
    pathMatch: 'full'
  },
  {
    path: '**',
    redirectTo: 'error404',
  }
];

1 Comment

Not work Even when I leave { path: 'tab-navigation', redirectTo: 'login', pathMatch: 'full' }, The result is the same.

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.