Reputation: 11162
In my Angular app, I would like users to be able to load it from root url (/
) only.
If the user tries any other url in the browser, he should be redirected to root url immediately.
However, class redirections like this.router.navigate(['some-url'])
in the app should work correctly
How can I do this?
For now, I saw NavGuard
in this post but maybe there is simpler trick to make this.
Thanks for your ideas.
Upvotes: 0
Views: 1384
Reputation: 933
Accepted answer is a bad practice imho
You should try this routing module:
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { BarComponent } from '@components/bar.component';
const routes: Routes = [
{
path: '**',
redirectTo: '',
},
{
path: '',
component: BarComponent,
}
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule],
})
export class FooRoutingModule { }
Upvotes: 1
Reputation: 1338
you can just add in your app.module constructor a router.navigateByUrl('/') therefore no matter what they do, when the app loads they go there. Or you could create a variable in your sessionStorage that holds a value, such as sessionStorage.setItem('userOnline', true)
, and then check for that value OnInit. Since session storage is destroyed on tab-close, then they can route-navigate if putting any route directly over the broswer navbar, but ONLY if they already have the app loaded. App loading would redirect to '/'.
Any variation of this idea, could and should work. Do ask me if you are lost though!
Upvotes: 2