2

Component Routing of my app

I have setup my routing like that. When user browse to http://localhost:3000, if tries to load Home component. But if user hasn't logged, it redirect user to login component.

@Component({
    moduleId: module.id,
    selector: 'app',
    templateUrl: 'app.component.html',
    styleUrls: ['app.component.css'],
    directives: [ROUTER_DIRECTIVES],
    providers: [UserService]
})
@Routes([
    {path: '/', component: HomeComponent},
    {path: '/login', component: LoginComponent}
])
export class App implements OnInit {

    title: string = 'App Component';

    constructor(private router: Router) {}

    ngOnInit() {
    }
}

Up to that point it is working fine. No issue.

Once user login, it redirect user to home component.

But when I try to load /comp1 with the URL http://localhost:3000/comp1, it looks at second level and try to find /comp1 along with home and login component rather than under 'Home component' and throwing an error that no route '/comp1' found.

I understand that it is an ambiguity issue. Because my home url is '/'. But this is very common that home component sitting at root level of your app and you dont want to add with '/home' or '/main' with your root url for your home component.

Any suggestion how to make this working?

peterh
  • 11,875
  • 18
  • 85
  • 108
microchip78
  • 2,040
  • 2
  • 28
  • 39
  • For your comp1, can you try the path as /home/comp1? – Madhu Ranjan May 17 '16 at 00:15
  • Hi Madhu, I know I can do that ... I have already mentioned that in my question ... but I dont want to add /home or /main or anything like that ... I prefer my route to called **http://localhost:3000/comp1** rather than **http://localhost:3000/home/comp1** not appropriate for me ... thats why I am asking question here ... – microchip78 May 17 '16 at 05:03
  • Maybe when they get `useAsDefault` working ([doc](https://angular.io/docs/ts/latest/guide/router.html#!#child-router))... Is there a specific reason you need that extra layer and don't put `login` at the same routing level as `comp1` and `comp2`? – Jason Goemaat May 17 '16 at 05:41
  • There is no technical reason ... it is just a logical app component make a decision whether to load `login` or `home` based user logged in or not ? and once user in `home` then user navigate to either `comp1` or `comp2` – microchip78 May 19 '16 at 00:23

0 Answers0