当用户尝试访问/ 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)
];
答案 0 :(得分:0)
更改
{path: '/**', redirectTo: '/', pathMatch: 'full' }
到
{path: '**', redirectTo: '/', pathMatch: 'full' }
路径不应包含前导/