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

相关内容

热门资讯

透视方式!菠萝辅助器免费版的功... 透视方式!菠萝辅助器免费版的功能介绍,约局吧开挂(透视)切实真的有脚本工具(哔哩哔哩)所有人都在同一...
透视技法!aapoker免费透... 透视技法!aapoker免费透视脚本,aapoker插件下载(透视)真是有脚本神器(哔哩哔哩)透视技...
透视大纲!hhpoker必备开... 透视大纲!hhpoker必备开挂,hhpoker作必弊码怎么用(透视)原来是有脚本教程(哔哩哔哩)1...
透视指南!aapoker如何设... 透视指南!aapoker如何设置胜率,aa poker辅助(透视)一贯真的有脚本脚本(哔哩哔哩)1、...
透视绝活儿!wpk俱乐部有没有... 透视绝活儿!wpk俱乐部有没有辅助,wpk有辅助器吗(透视)原来存在有透视神器(哔哩哔哩)wpk俱乐...
透视方式!wpk是真的还是假的... 透视方式!wpk是真的还是假的,wpk俱乐部有没有辅助(透视)真是有透视技巧(哔哩哔哩)1、用户打开...
透视学习!wepoker私人局... 透视学习!wepoker私人局规律,wepoker插件程序激活码(透视)果然有透视方法(哔哩哔哩)1...
透视办法!wepoker插件功... 透视办法!wepoker插件功能辅助器,wepoker免费透视脚本(透视)原来是真的透视方法(哔哩哔...
透视教材!aapoker怎么提... 透视教材!aapoker怎么提高中牌率,aapoker能控制牌吗(透视)确实真的有透视插件(哔哩哔哩...
透视办法!wpk模拟器多开,w... 透视办法!wpk模拟器多开,wpk安卓下载辅助(透视)确实有透视软件(哔哩哔哩)1、每一步都需要思考...