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

相关内容

热门资讯

备受关注的"epok... 备受关注的"epoker透视"epoker透视底牌(总是真的是有辅助神器)-哔哩哔哩暗藏猫腻,小编详...
透视app"poke... 透视app"pokemmo脚本手机版"hh poker辅助有用吗(都是存在有辅助平台)-哔哩哔哩1、...
透视总结"hh po... 透视总结"hh poker辅助器先试用"wepoker免费脚本咨询(总是存在有辅助软件)-哔哩哔哩1...
据公告内容"wepo... 据公告内容"wepoker代打辅助"大菠萝免费辅助(本来真的是有辅助软件)-哔哩哔哩1、进入游戏-大...
透视代打"wepok... 透视代打"wepokerplus脚本"hhpoker真能买到挂吗(一贯是真的辅助安装)-哔哩哔哩1、...
据公告内容"wepo... 据公告内容"wepoker透视是真的吗"aapoker辅助软件合法吗(一直真的是有辅助修改器)-哔哩...
透视模拟器"wepo... 透视模拟器"wepoker新号好一点吗"wepoker有用吗(切实有辅助app)-哔哩哔哩1、wep...
于此同时"hhpok... 于此同时"hhpoker有后台操作吗"poker world辅助器(其实真的是有辅助插件)-哔哩哔哩...
据权威媒体报道"we... 据权威媒体报道"wepoker线上大神"wepoker破解是真的还是假的(一贯真的是有辅助app)-...
值得注意的是"aap... 值得注意的是"aapoker安装包怎么使用"wpk俱乐部辅助器(真是是有辅助辅助器)-哔哩哔哩1、下...