首页 文章

在Angular2中,如果路径不存在,如何重定向到主页

提问于
浏览
1

如果路由不存在,如何重定向到主页 . 例如/ test将重定向到主页 .

*我知道我使用的是较旧版本的路由器,但目前我没有时间进行升级,需要使用此版本的解决方案 .

This is my version: "@angular/router": "2.0.0-rc.1"

Example of my current routes in my app.ts file:

@Routes([
  { path: '/', component: Home},
  { path: '/home', component: Home, useAsDefault: true },
  { path: '/login', component: Login },      
  { path:'/**', redirectTo: ['Login']}
])

我正在使用angular2和打字稿 .

我这个控制台错误说: Error: Uncaught (in promise): Cannot match any routes.

我的引导代码:

bootstrap(AppComponent, [
    ROUTER_PROVIDERS,
    HTTP_BINDINGS,
    provide(APP_BASE_HREF, { useValue: '<%= APP_BASE %>' }),
    BootFlatDatePicker,
    ANGULAR2_GOOGLE_MAPS_PROVIDERS,
    Environment,
    SocialService,
    AuthService,
    UserService,
    OrganisationService,
    NotificationService,
    ApplicationService,
    JobService,
    MessageService,
    EmailService
]);

2 回答

  • 1

    在结束时添加此路线 .

    {path:'**', component: Login }
    
  • 0

    这是在旧路由器版本中执行此操作的方法:

    {path:'*',组件:登录}

相关问题