Angular 2 - 404页面未显示

时间:2017-01-01 23:06:51

标签: javascript angular typescript routing angular2-routing

当用户输入的网址与我的网络应用中的任何路径不匹配时,我设置了“404页面未找到”页面:

export const routerConfig: Routes = [
   {
      component: LandingPageComponent,
      path: "",
   },
   {
      component: ProfilePageComponent,
      path: "profile",
   },
   {
      component: NotFoundPageComponent,
      path: "**",
   },
];

但是当我输入我的应用地址然后输入错误的路径(例如http://localhost:3000/prcds)时,它只会显示:

Cannot GET /prcds

在控制台出错的空白网页中:

  

无法加载资源:服务器响应状态为404   (未找到)

我已将NotFoundPageModule添加到app.module个导入中。

我使用了https://angular.io/docs/ts/latest/guide/router.html中的“配置”部分,并复制了他们如何在我的项目中设置404未找到的页面。

如何让它停止显示该错误并显示我为404错误制作的实际页面?

它显示了我的其他问题中的相同页面,因此它与this issue相关的可能性很小:

我的代码:

找不到页面文件结构:

enter image description here

notfoundpage.component.ts

import { Component } from "@angular/core";
import { Title } from "@angular/platform-browser";

@Component({
   selector: "not-found-page",
   styleUrls: ["dist/app/components/not-found-page/not-found-page.component.css"],
   templateUrl: "dist/app/components/not-found-page/not-found-page.component.html",
})
export class NotFoundPageComponent {
   constructor(private titleService: Title) {
      this.titleService.setTitle("Not Found");
   }
}

notfoundpage.module.ts

import { CommonModule } from "@angular/common";
import { NgModule } from "@angular/core";
import { FormsModule } from "@angular/forms";
import * as NotFoundPage from "./index";

@NgModule({
   declarations: [
      NotFoundPage.NotFoundPageComponent,
   ],
   exports: [NotFoundPage.NotFoundPageComponent],
   imports: [CommonModule, FormsModule],
})
export class NotFoundPageModule { }

app.module.ts

// tslint:disable-next-line:no-reference
/// <reference path="../../../node_modules/moment/moment.d.ts" />

// Angular 2 modules
import { Component, NgModule } from "@angular/core";
import {
   ControlValueAccessor,
   FormBuilder,
   FormGroup,
   FormsModule,
   ReactiveFormsModule,
   Validators,
} from "@angular/forms";
import { BrowserModule } from "@angular/platform-browser";
import { RouterModule } from "@angular/router";
import { AgmCoreModule } from "angular2-google-maps/core";
import { routerConfig } from "../app.routes";

// Plugin modules
import { Ng2Bs3ModalModule } from "ng2-bs3-modal/ng2-bs3-modal";

// App modules
import { AboutPageModule } from "../components/about-page/about-page.module";
import { AddPageModule } from "../components/add-page/add-page.module";
import { FindPageModule } from "../components/find-page/find-page.module";
import { LandingPageModule } from "../components/landing-page/landing-page.module";
import { NotFoundPageModule } from "../components/not-found-page/not-found-page.module";
import { ProfilePageModule } from "../components/profile-page/profile-page.module";
import { RegisterPageModule } from "../components/register-page/register-page.module";
import { SharedModule } from "../components/shared/shared.module";
import { AppComponent } from "./app.component";

@NgModule({
   bootstrap: [AppComponent],
   declarations: [
      AppComponent,
   ],
   imports: [
      BrowserModule,
      RouterModule.forRoot(routerConfig),
      FormsModule,
      Ng2Bs3ModalModule,
      AgmCoreModule,
      ReactiveFormsModule,
      LandingPageModule,
      FindPageModule,
      AddPageModule,
      AboutPageModule,
      RegisterPageModule,
      ProfilePageModule,
      NotFoundPageModule,
      SharedModule,
   ],
   providers: [
      FormBuilder,
   ],
})
export class AppModule { }

整个app.routes.ts:

import { Routes } from "@angular/router";
import { AboutPageComponent } from "./components/about-page/about-page.component";
import { AddPageComponent } from "./components/add-page/add-page.component";
import { FindPageComponent } from "./components/find-page/find-page.component";
import { LandingPageComponent } from "./components/landing-page/landing-page.component";
import { NotFoundPageComponent } from "./components/not-found-page/not-found-page.component";
import { ProfilePageComponent } from "./components/profile-page/profile-page.component";
import { RegisterPageComponent } from "./components/register-page/register-page.component";

export const routerConfig: Routes = [
   {
      component: LandingPageComponent,
      path: "",
   },
   {
      path: "",
      pathMatch: "full",
      redirectTo: "",
   },
   {
      component: FindPageComponent,
      path: "find",
   },
   {
      component: AddPageComponent,
      path: "add",
   },
   {
      component: RegisterPageComponent,
      path: "register",
   },
   {
      component: AboutPageComponent,
      path: "about",
   },
   {
      component: ProfilePageComponent,
      path: "profile",
   },
   {
      path: "**",
      pathMatch: "full",
      redirectTo: "NotFoundPageComponent",
   },
   {
      component: ProfilePageComponent,
      path: "**",
   },
];

2 个答案:

答案 0 :(得分:4)

使用以下代码替换app.routes.ts文件:

&#13;
&#13;
import { Routes, RouterModule } from "@angular/router";
import { AboutPageComponent } from "./components/about-page/about-page.component";
import { AddPageComponent } from "./components/add-page/add-page.component";
import { FindPageComponent } from "./components/find-page/find-page.component";
import { LandingPageComponent } from "./components/landing-page/landing-page.component";
import { NotFoundPageComponent } from "./components/not-found-page/not-found-page.component";
import { ProfilePageComponent } from "./components/profile-page/profile-page.component";
import { RegisterPageComponent } from "./components/register-page/register-page.component";

const routerConfig: Routes = [
   {
      component: LandingPageComponent,
      path: "",
   },
   {
      component: FindPageComponent,
      path: "find",
   },
   {
      component: AddPageComponent,
      path: "add",
   },
   {
      component: RegisterPageComponent,
      path: "register",
   },
   {
      component: AboutPageComponent,
      path: "about",
   },
   {
      component: ProfilePageComponent,
      path: "profile",
   },
   {
      component: NotFoundPageComponent,
      path: "404",
   },
   {
      path: "**",
      redirectTo: '404' 
   }
];

export const routerConfig = RouterModule.forRoot(routerConfig);
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您需要提供pathMatch:'full',如下所示:

        export const routerConfig: Routes = [
           {
              component: LandingPageComponent,
              path: "",
           },
           {
              component: ProfilePageComponent,
              path: "profile",
           },
            { path: '', redirectTo: 'NotFoundPageComponent', pathMatch: 'full' },
           {
              component: NotFoundPageComponent,
              path: "**",
           },
        ];

在“notfoundpage.module.ts”中,请尝试以下操作:

替换它:

       import * as NotFoundPage from "./index";

有了这个:

        import {NotFoundPageComponent } from './not-found-page.component';

并替换它:

     declarations: [
          NotFoundPage.NotFoundPageComponent,
       ],
       exports: [NotFoundPage.NotFoundPageComponent]

有了这个:

         declarations: [
              NotFoundPageComponent,
           ],
           exports: [NotFoundPageComponent]