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

相关内容

热门资讯

程序员教你!hhpoker万能... 程序员教你!hhpoker万能辅助器(辅助挂)真是是有挂(有挂规律辅助插件)1、很好的工具软件,可以...
记者揭秘!wepoker有脚本... 记者揭秘!wepoker有脚本吗(辅助挂)本来真的有挂(有挂详细辅助工具)该软件可以轻松地帮助玩家将...
科普分享!hhpoker哪个俱... 科普分享!hhpoker哪个俱乐部靠谱(辅助挂)都是存在有挂(有挂方法辅助工具)1、上手简单,内置详...
必备科技!hhpoker的辅助... 必备科技!hhpoker的辅助是真的吗(辅助挂)确实是有挂(有挂神器辅助工具)是不是有人用挂微扑克w...
我来教教你!aapoker辅助... 我来教教你!aapoker辅助工具安全吗(辅助挂)好像存在有挂(有挂头条辅助脚本)1、游戏颠覆性的策...
科技分享!aapoker辅助器... 科技分享!aapoker辅助器是真的吗(辅助挂)原来存在有挂(有挂透视辅助器)运辅助工具,进入游戏界...
解密关于!pokemmo手机脚... 解密关于!pokemmo手机脚本辅助器(辅助挂)一贯有挂(有挂神器辅助攻略)1)辅助插件:进一步探索...
必备科技!wepoker怎么设... 必备科技!wepoker怎么设置盖牌(辅助挂)一直真的有挂(有挂方式辅助脚本)1)有没有挂:进一步探...
专业讨论!wepoker透视脚... 专业讨论!wepoker透视脚本下载(辅助挂)都是真的有挂(有挂规律辅助工具)1、每一步都需要思考,...
大神推荐!wepoker辅助器... 大神推荐!wepoker辅助器安装包(辅助挂)原来有挂(有挂方针辅助软件)是不是有人用挂微扑克wpk...