angular2路由redirectTo当url不存在时

时间:2016-08-18 09:50:25

标签: angular

当用户尝试访问/ randomUrl等不存在的网址时,是否有办法将用户重定向回主页。

export const routes: RouterConfig = [
 { path: '', component: LoginComponent },
];

我认为它会像下面的代码,但它不会重定向回主页。

{path: '/**', redirectTo: '/', pathMatch: 'full' }

full route.ts

import { Component } from '@angular/core';
import { provideRouter, RouterConfig, CanActivate } from '@angular/router';
import { LoginComponent } from '../login/component/login';
import { AuthGuard } from './authguard';
import { JourneysComponent } from '../journeys/components/journeys';

export const routes: RouterConfig = [
  { path: '', component: LoginComponent },
  {path:  '**', redirectTo: '/', pathMatch: 'full'},
  { path: 'journeys', component: JourneysComponent, canActivate: [AuthGuard] }

];

export const APP_ROUTER_PROVIDERS = [
  provideRouter(routes)
];

1 个答案:

答案 0 :(得分:0)

更改

{path: '/**', redirectTo: '/', pathMatch: 'full' }

{path: '**', redirectTo: '/', pathMatch: 'full' }

路径不应包含前导/