简体   繁体   English

url不存在时的angular2路由redirectTo

[英]angular2 routing redirectTo when url doesn't exist

Is there a way to redirect the user back to home page when they try to access non-existent url like /randomUrl. 当用户尝试访问不存在的URL(例如/ randomUrl)时,是否可以将用户重定向回首页。

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

I thought it would be something like the code below, but its not redirect back to the main page. 我认为这将类似于下面的代码,但它不会重定向回主页。

{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)
];

Change 更改

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

to

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

The paths should not contain leading / 路径中不应包含前导/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM