Angular 2 routing - disable, redirect, translation

≯℡__Kan透↙ 提交于 2019-12-12 02:37:11

问题


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


回答1:


   // 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']);
    }


来源:https://stackoverflow.com/questions/37677314/angular-2-routing-disable-redirect-translation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!