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

相关内容

热门资讯

透视阶段!wepoker轻量版... 透视阶段!wepoker轻量版透视,hhpoker德州机器人(透视)其实存在有挂(哔哩哔哩)1、完成...
透视妙计!购买wepoker模... 透视妙计!购买wepoker模拟器,wpk透视辅助(透视)好像是有挂(哔哩哔哩)进入游戏-大厅左侧-...
透视办法!epoker透视底牌... 透视办法!epoker透视底牌,佛手大菠萝辅助(透视)果然真的有脚本挂(哔哩哔哩)一、佛手大菠萝辅助...
透视讲义!wpk有那种辅助吗,... 您好,wpk有那种辅助吗这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多...
透视办法!wepoker的辅助... 透视办法!wepoker的辅助器,cloudpoker作必弊(透视)本来有挂(哔哩哔哩)1、在wep...
透视机巧!we-poker辅助... 透视机巧!we-poker辅助,德普之星辅助器怎么用(透视)竟然真的是有挂(哔哩哔哩)1、德普之星辅...
透视手段!aa poker辅助... 透视手段!aa poker辅助,aapoker透视脚本入口(透视)原来真的是有脚本软件(哔哩哔哩)1...
透视方针!hhpoker软件靠... 透视方针!hhpoker软件靠谱吗,wepoker买钻石有用吗(透视)其实是真的挂(哔哩哔哩)1)w...
透视讲义!werplan外开挂... 透视讲义!werplan外开挂,哈糖大菠萝能开挂吗(透视)本来是有透视app(哔哩哔哩)小薇(辅助器...
透视攻略!wpk插件,wpk透... 透视攻略!wpk插件,wpk透视辅助下载(透视)竟然存在有透视app(哔哩哔哩)1、下载好wpk透视...