notFound页面的angular2路由 [英] angular2 route for notFound page

查看:135
本文介绍了notFound页面的angular2路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用儿童溃烂。我的主要路由模块(在根目录中):

I try to use children routs. My main routing-module(in root directory):

import { NgModule }             from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { HomeComponent } from './home/home.component';
import { ProfileComponent } from './profile/profile.component';
import { NotFoundComponent } from './notfound/notfound.component';
import { LoggedInGuard } from './login-guard/login-guard.component';

const routes: Routes = [
    {path: '', redirectTo: 'home', pathMatch: 'full'},
    {path: 'home', component: HomeComponent, pathMatch: 'full'},
    {path: 'profile', component: ProfileComponent, canActivate: [LoggedInGuard], canLoad: [LoggedInGuard]},
    {path: '**', component: NotFoundComponent},
];
@NgModule({
    imports: [ RouterModule.forRoot(routes) ],
    exports: [ RouterModule ]
})
export class AppRoutingModule {}

和子路由模块(在子目录中):

and child routing-module(in sub-directory):

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

import { PPComponent } from './pp.component';
import { MembersComponent } from './members/members.component';
import { HistoryComponent } from './history/history.component';


@NgModule({
  imports: [
    RouterModule.forChild([
      {
        path: 'partner-program',
        component: PPComponent,
        children: [
          {
            path: '',
            redirectTo: 'members',
            pathMatch: 'full'
          },
          {
            path: 'members',
            component: MembersComponent,
          },
          {
            path: 'history',
            component: HistoryComponent,
          },
        ]
      },


    ])
  ],
  exports: [
    RouterModule
  ]
})
export class PPRoutingModule { }

我对此路线 {path:'**',component:NotFoundComponent} 有疑问。 Angular2会在任何子级路线之前看到此路线。结果,URL‘ http:// localhost:3000 / partner-program 显示未找到组件。如果我删除notFound路由,则‘ http:// localhost:3000 / partner-program 可以正常工作。
如何声明notFound路线并说Angular2在最后一轮(在子路线之后)检查它?

I have a question about this route {path: '**', component: NotFoundComponent}. Angular2 sees this route before any children routes. And as a result url 'http://localhost:3000/partner-program' shows notFound component. If I remove notFound route, 'http://localhost:3000/partner-program' works fine. How can I declare notFound route and say Angular2 check it in last turn(after child routes)?

GünterZöchbauer,您是说这样吗?

Günter Zöchbauer, do you mean smth like this?

RouterModule.forChild([
      {
        path: '',
        children: [
          {
            path: 'partner-program',
            component: PPComponent,
            children: [
              {
                path: '',
                redirectTo: 'members',
                pathMatch: 'full'
              },
              {
                path: 'members',
                component: MembersComponent,
              },
              {
                path: 'history',
                component: HistoryComponent,
              },
            ]
          }
        ]
      }
    ])


推荐答案

将这些模块导入到麦在模块中,请确保最后添加AppRoutingModule。这都是关于路线注册的顺序

When you import these modules into the main module please make sure that AppRoutingModule is added on the end. It's all about the order of routes registration

这篇关于notFound页面的angular2路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆