本文介绍了Angular 2路由-禁用,重定向,翻译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Angular 2应用中,我具有此Route配置.

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 }
])

如果用户键入URL地址 http://localhost:8080/receipt 和其他(付款等)我需要将他/她重定向到根页面(/).无法进入收据页面,因为他/她没有选择门票类型.那么,如何为路由/重定向创建一些规则?例如,应该允许/ticket.

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.

我还需要解决app的本地化问题-现在我在AppComponent的构造函数(后端的GET请求)中翻译app,但是如果用户直接进入/ticket页面,则不会调用AppComponent的翻译-无法翻译app.谢谢

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

推荐答案

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

这篇关于Angular 2路由-禁用,重定向,翻译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 17:26