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

相关内容

热门资讯

重大科普!闲逸亲友圈app辅助... 重大科普!闲逸亲友圈app辅助(辅助挂)wpk透视工作室(素来真的有挂);闲逸亲友圈app辅助软件透...
总算了解!来物局潮汕辅助(辅助... 总算了解!来物局潮汕辅助(辅助挂)德州局HHpoker透视脚本(原先有挂);总算了解!来物局潮汕辅助...
科普分享!微信边锋辅助(辅助挂... 科普分享!微信边锋辅助(辅助挂)wpk辅助是什么(竟然有挂);大家肯定在之前微信边锋辅助或者微信边锋...
一分钟带你了解!钱塘十水三攻略... 一分钟带你了解!钱塘十水三攻略(辅助挂)hhpoker真的假的(原先真的有挂);超受欢迎的钱塘十水三...
玩家必备攻略!蜜瓜大厅辅助免费... 玩家必备攻略!蜜瓜大厅辅助免费下载(辅助挂)wepoker正确养号方法(原先有挂);玩家必备攻略!蜜...
科普攻略!蜀山四川小程序脚本(... 《科普攻略!蜀山四川小程序脚本(辅助挂)hh poker辅助有用吗(都是是真的有挂)》 蜀山四川小程...
一分钟秒懂!先锋脚本辅助(辅助... 一分钟秒懂!先锋脚本辅助(辅助挂)wepoker钻石怎么看底牌(原生存在有挂);先锋脚本辅助中的10...
技术分享!点点游戏辅助(辅助挂... 技术分享!点点游戏辅助(辅助挂)sohoo poker辅助器(好像是有挂);详细点点游戏辅助攻略(点...
每日必看推荐!水鱼辅助软件下载... 每日必看推荐!水鱼辅助软件下载(辅助挂)佛手大菠萝13道挂哪里(从前是有挂)是一款可以让一直输的玩家...
每日必备!小程序微乐游戏辅助器... 每日必备!小程序微乐游戏辅助器(辅助挂)wepoker轻量版有透视吗(果然真的是有挂)是一款可以让一...