Angular路由只将某些路由路由到404页面,不会将子路由错误路由到404页面。
创始人
2024-10-28 05:31:09
0

可以使用控制台打印来检查错误信息,然后将所有错误路由到404页面,包括子路由的错误。以下是一个示例代码:

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { NotFoundComponent } from './not-found/not-found.component';

const routes: Routes = [
  {
    path: '',
    loadChildren: () => import('./home/home.module').then(m => m.HomeModule)
  },
  {
    path: '**',
    component: NotFoundComponent
  }
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

在上面的代码中,NotFoundComponent是你的404组件,如果路由无法匹配任何其他路由,则会自动路由到该组件。

然后可以在子路由中添加一个通配符路由来使普通路由错误也能被捕获并路由到404页面:

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { NotFoundComponent } from '../not-found/not-found.component';
import { ChildComponent } from './child.component';

const routes: Routes = [
  {
    path: '',
    component: ChildComponent
  },
  {
    path: '**',
    component: NotFoundComponent
  }
];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule]
})
export class ChildRoutingModule { }

这样,当访问不存在的子路由时,它将路由到404页面。

相关内容

热门资讯

透视输赢!约局吧是否有挂(透视... 透视输赢!约局吧是否有挂(透视)开挂辅助软件(其实是有挂)-哔哩哔哩约局吧是否有挂辅助器中分为三种模...
透视代打!来玩德州破解器,we... 透视代打!来玩德州破解器,we-poker辅助器,手册教程(总是真的有挂)-哔哩哔哩1、来玩德州破解...
透视辅助挂!拱趴大菠萝万能辅助... 透视辅助挂!拱趴大菠萝万能辅助器(透视)开挂辅助脚本(一贯是真的挂)-哔哩哔哩1、拱趴大菠萝万能辅助...
透视挂!wpk有那种辅助吗(透... 透视挂!wpk有那种辅助吗(透视)开挂辅助攻略(本来有挂)-哔哩哔哩1、下载好wpk有那种辅助吗辅助...
透视科技!德普之星辅助器app... 透视科技!德普之星辅助器app,aa poker透视软件,技法教程(一贯是有挂)-哔哩哔哩1、操作简...
透视存在!竞技联盟辅助(透视)... 透视存在!竞技联盟辅助(透视)开挂辅助app(本来是有挂)-哔哩哔哩1、每一步都需要思考,不同水平的...
透视脚本!德普之星透视辅助,h... 透视脚本!德普之星透视辅助,hhpoker软件可以玩吗,绝活儿教程(原来是真的挂)-哔哩哔哩hhpo...
透视代码!aapoker透视怎... 透视代码!aapoker透视怎么用(透视)开挂辅助插件(果然是有挂)-哔哩哔哩1、玩家可以在aapo...
透视工具!aapoker破解侠... 透视工具!aapoker破解侠是真的吗,wepoker私人局怎么玩,方案教程(一贯存在有挂)-哔哩哔...
透视苹果!hhpoker真能买... 透视苹果!hhpoker真能买到挂吗(透视)开挂辅助挂(确实有挂)-哔哩哔哩1、首先打开hhpoke...