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透视底牌脚本(透视)微信小程序边锋辅助(其实是真的辅助软件)-哔哩哔哩1、...
透视透视!wejoker辅助脚... 透视透视!wejoker辅助脚本(透视)决战十水三辅助(其实存在有辅助平台)-哔哩哔哩亲,关键说明,...
最新消息!pokemmo辅助器... 最新消息!pokemmo辅助器手机版下载(透视)湖北逍遥卡五星辅助(切实真的有辅助平台)-哔哩哔哩1...
透视玄学!wejoker免费脚... 透视玄学!wejoker免费脚本(透视)悠闲游戏辅助(本来是有辅助app)-哔哩哔哩1、悠闲游戏辅助...
据相关数据显示!wpk透视辅助... 据相关数据显示!wpk透视辅助(透视)开心十三张有没有挂(原来存在有辅助平台)-哔哩哔哩运开心十三张...
此事备受玩家关注!werpla... 此事备受玩家关注!werplan怎么作必弊(透视)微信小程序微乐破解器(一直有辅助下载)-哔哩哔哩1...
今年以来!wepoker私局代... 今年以来!wepoker私局代打(透视)潮汕汇app透视软件(一直是真的辅助工具)-哔哩哔哩1、实时...
今日!wpk作必弊是真的吗(透... 今日!wpk作必弊是真的吗(透视)随意玩有没有辅助(切实真的是有辅助修改器)-哔哩哔哩1、进入到随意...
透视教程!wepoker辅助器... 透视教程!wepoker辅助器最新版本更新内容(透视)超级三加一辅助工具安装(其实有辅助工具)-哔哩...
透视系统!大菠萝789辅助器下... 透视系统!大菠萝789辅助器下载(透视)兴动互娱辅助贴吧开挂(真是是真的辅助辅助器)-哔哩哔哩兴动互...