0

My router is like this.

@NgModule({
imports: [
    RouterModule.forChild([
        {
            path: '',
            component: AppComponent,
            children: [
                {path: '', redirectTo: '/home', pathMatch: 'full'},
                { path: 'home', component: HomeComponent,  canActivate: [AppRouteGuard] },
                { path: 'books', component: BooksComponent, data: { permission: 'Pages.Books' }, canActivate: [AppRouteGuard] }, 
                { path: 'books/detail/:id', component: BooksComponent, data: { permission: 'Pages.Books' }, canActivate: [AppRouteGuard]},                   
                { path: '**', redirectTo: '/app/home'}                   
            ]
        }
    ])
],
exports: [RouterModule]

})

So when a user select a book from book page send book id to get details and url will be like

http://localhost:4200/app/prospects/detail/2045

But I don't want a user to type this url manually to get the details. Is there any way to redirect to home if a user types url manually?

sally
  • 379
  • 2
  • 8
  • 17
  • No you can't prevent user to enter the url and load specific component defined in router for that url – Kiran Shinde Sep 27 '19 at 09:08
  • True, its impossible. But what do you want to avoid with this? Failure in detail page? If you have a detail call in the page and that call to the serverfails, with not found. You could navigate automatically to another page by writing the navigate in the error callback. But I don't think its a good idea. Its better to show the user that the detail could not be loaded – Jens Alenius Sep 27 '19 at 09:14
  • 1
    Possible duplicate of [Prevent routing in Angular when user manually changes url in browser tab](https://stackoverflow.com/questions/36277506/prevent-routing-in-angular-when-user-manually-changes-url-in-browser-tab) – leopal Sep 27 '19 at 10:25
  • That's not true, it's totally possible to prevent that. Do not listen to them. –  Sep 27 '19 at 10:27
  • But why would you do something like that ? What's your use-case ? –  Sep 27 '19 at 10:28
  • @leopal while it's indeed a duplicate, I'm not sure the other question has an answer. It's just a documentation explanation, but it doesn't really give the answer to the issue ... –  Sep 27 '19 at 10:30

0 Answers0