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页面。

相关内容

热门资讯

WPK下载手册!哈糖大菠萝破解... WPK下载手册!哈糖大菠萝破解器,WePoKerai多功能透视插件;1、玩家可以在哈糖大菠萝破解器线...
WPK靠谱方式!fishpok... WPK靠谱方式!fishpoker透视,wpkai多功能辅助器app1、全新机制【fishpoker...
WPK插件指引!拱趴大菠萝挂哪... WPK插件指引!拱趴大菠萝挂哪里,aapokerai多功能开挂软件1、全新机制【拱趴大菠萝挂哪里ai...
WPK安卓总结!德州私人局怎么... WPK安卓总结!德州私人局怎么透视,wpkai多功能开挂脚本1)德州私人局怎么透视免费钻石:进一步探...
WPK透视妙计!德扑之星透视辅... WPK透视妙计!德扑之星透视辅助插件,aapokerai多功能开挂方法;1、每一步都需要思考,不同水...
WPK插件窍要!wpk有辅助器... WPK插件窍要!wpk有辅助器吗,AApokerai多功能辅助攻略1)wpk有辅助器吗有没有挂:进一...
WPK真假演示!德州辅助工具到... WPK真假演示!德州辅助工具到底怎么样,菠萝智星德州ai多功能透视挂技巧1、下载好德州辅助工具到底怎...
WPK官网法门!来玩app破解... WPK官网法门!来玩app破解版,WePoKerai多功能透视app1、在来玩app破解版插件功能辅...
WPK作弊积累!菠萝辅助器免费... WPK作弊积累!菠萝辅助器免费版的特点,AApokerai多功能透视挂神器菠萝辅助器免费版的特点脚本...
WPK安卓方式!德州局脚本,w... WPK安卓方式!德州局脚本,wpkai多功能透视神器德州局脚本透视方法中分为三种模型:德州局脚本靠谱...