简体   繁体   English

如何在 Angular 项目中为自定义 404 页面设置路由

[英]How to set up a route for custom 404 page in Angular Project

In my Angular project I have a component called 'wrongRouteComponent' for custom 404 page.在我的 Angular 项目中,我有一个名为“wrongRouteComponent”的组件,用于自定义 404 页面。 When someone entered a non Pre-defined route, It should show the 'wrong-route.component.html'.当有人输入非预定义路由时,它应该显示'wrong-route.component.html'。 I do not understand how to do this.我不明白如何做到这一点。

This is the 'app-rouring.module.ts' that I have used.这是我使用过的“app-routing.module.ts”。

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

// Required components for which route services to be activated
import { LandingComponent } from '../../components/landing/landing.component';
import { DashboardComponent } from '../../components/dashboard/dashboard.component';
import { UserprofileComponent } from '../../components/userprofile/userprofile.component';
import { WrongRouteComponent } from '../../components/wrong-route/wrong-route.component';

const routes: Routes = [
  { path: '', component: LandingComponent},
  { path: '/dashboard', component: DashboardComponent},
  { path: '/userprofile', component: UserprofileComponent},

  //Wrong route
  { path: '404', component: WrongRouteComponent},

];

Can anyone tell me what changes should I add to the谁能告诉我应该添加哪些更改

{ path: '404', component: WrongRouteComponent},

You can simpally do it你可以简单地做到

{
    path        : '**',
    pathMatch   : 'full',
    component   : WrongRouteComponent
}

Put this wildcard route in your routing file at last in routes array.最后将此通配符路由放入路由文件中的路由数组中。

I hope it helps you.我希望它能帮助你。

Please change path '404' to **请将路径'404'更改为**

So,the routes should be所以,路线应该是

const routes: Routes = [
  { path: '', component: LandingComponent},
  { path: '/dashboard', component: DashboardComponent},
  { path: '/userprofile', component: UserprofileComponent},

  //Wrong route
  { path: '**', component: WrongRouteComponent},

];

wild card route will catch all paths that you don't know (undefined routes in your constant). wild card route将捕获您不知道的所有路径(常量中未定义的路由)。 Undefined URL causes the router to throw an error and crash the app.未定义的 URL 会导致路由器抛出错误并使应用程序崩溃。

Normally routes are read from top to bottom, that's why it's important to keep wild card route ('**') at the end of the route list.通常路由是从上到下读取的,这就是为什么在路由列表的末尾保留通配符路由 ('**') 很重要的原因。 If you kept this on top, all the other routes will be taken as undefined.如果你把它放在最上面,所有其他路由都将被视为未定义。

You can use it as below.您可以按如下方式使用它。

{path: ‘not-found’ , component: ‘NotFoundComponent’}
{path: ‘**’ , redirectTo: ‘/not-found’}

Or或者

 { path: '**', component: NotFoundComponent}

This is the answer that worked for me.这是对我有用的答案。

const routes: Routes = [
  { path: '', component: LandingComponent},
  { path: '/dashboard', component: DashboardComponent},
  { path: '/userprofile', component: UserprofileComponent},

  //Wild Card Route
  { path: '**', pathMatch   : 'full', component: WrongRouteComponent},

];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM