Loutocký
Loutocký

Reputation: 842

Angular 2 routing - disable, redirect, translation

In my Angular 2 app I have this Route config.

@RouteConfig([
    { path: '/', name: 'Welcome', component: WelcomeComponent, useAsDefault: true },
    { path: '/ticket', name: 'Ticket', component: TicketComponent },
    { path: '/payment', name: 'Payment', component: PaymentComponent },
    { path: '/receipt', name: 'Receipt', component: ReceiptComponent },
    { path: '/login', name: 'Login', component: LoginComponent },
    { path: '/services', name: 'Services', component: ServicesComponent },
    { path: '/email', name: 'Email', component: EmailComponent },
    { path: '/used-services', name: 'UsedServices', component: UsedServicesComponent }
])

If the user type URL address http://localhost:8080/receipt and others (payment etc.) I need to redirect him/her to root page (/). It is not possible to go on receipt page, because he/she do not choose type of ticket. So how can I create some rules for routing/redirection? For example, /ticket should be allowed.

Also I need to solve localization of app - now I translate app in constructor (GET request on backend) of AppComponent, but if user goes on /ticket page directly, the translation of AppComponent is not invoked - app is not translated. Thanks

Upvotes: 0

Views: 2964

Answers (1)

mayur
mayur

Reputation: 3618

   // on type ticket can save that it localstorage like...

   localStorage.setItem('tickType', 'Bus');

   // and in receipt page check this local storage tickType exixt or not 

    if(localStorage.getItem('tickType')) {
            console.log(true);
    } else {
      console.log(false);
      this.router.navigate( ['/Welcome']);
    }

Upvotes: 1

Related Questions