【问题标题】:Cannot match any routes in Angular 2.1无法匹配 Angular 2.1 中的任何路由
【发布时间】:2017-03-07 17:27:10
【问题描述】:

我在 Visual Studio 2015 AspNetCore 项目中使用 Angular 2.1.0,并且在我的延迟加载模块中遇到路由问题。错误如下:

core.umd.js:3257 异常:未捕获(承诺中):错误:无法匹配 任何路线。 URL 段:'Home/MyList' 错误:无法匹配任何 路线。 URL 段:'Home/MyList' 在 ApplyRedirects.noMatchError (http://localhost:31534/libraries/@angular/router/bundles/router.umd.js:769:20) 在 CatchSubscriber.eval [作为选择器] (http://localhost:31534/libraries/@angular/router/bundles/router.umd.js:747:33) 在 CatchSubscriber.error (http://localhost:31534/libraries/rxjs/operator/catch.js:52:31) 在 MapSubscriber.Subscriber._error (http://localhost:31534/libraries/rxjs/Subscriber.js:128:26) 在 MapSubscriber.Subscriber.error (http://localhost:31534/libraries/rxjs/Subscriber.js:102:18) 在 MapSubscriber.Subscriber._error (http://localhost:31534/libraries/rxjs/Subscriber.js:128:26) 在 MapSubscriber.Subscriber.error (http://localhost:31534/libraries/rxjs/Subscriber.js:102:18) 在 MapSubscriber.Subscriber._error (http://localhost:31534/libraries/rxjs/Subscriber.js:128:26) 在 MapSubscriber.Subscriber.error (http://localhost:31534/libraries/rxjs/Subscriber.js:102:18) 在 LastSubscriber.Subscriber._error (http://localhost:31534/libraries/rxjs/Subscriber.js:128:26)

我已经关注Tour Of Heroes tutorial 并根据lazy load topic here 进行了改编 我首先在名为AppModule 的引导模块中加载登录页面{Home/Login}。单击登录时,我想显示下一页MyList{Home/MyList},它被延迟加载到名为MyModule 的功能模块中。

MyModule 基于上面第二个链接中的HeroModule

MyList 页面和相关组件是MyModule 下的第一个页面/组件,它也有一个MyListDetail 页面/组件{Home/MyListDetail}

我使用此代码在单击按钮时导航到 {Home/MyList}

let link = ['Home/MyList'];
this.router.navigate(link);

为什么找不到我的路线?这是我设置它们的方法。

app-routing.module.ts

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

export const routes: Routes = [
  { path: '', redirectTo: 'Home/Login', pathMatch: 'full'},
  { path: 'Home/MyList', loadChildren: 'scripts/My.Module#MyModule' }  

];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule {}

login-routing.module.ts

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

import { LoginCmpnt }  from './Login';

@NgModule({
  imports: [RouterModule.forChild([
    { path: 'Home/Login', component: LoginCmpnt}
  ])],
  exports: [RouterModule]
})
export class LoginRoutingModule {}

MyList-routing.module.ts

import { NgModule }            from '@angular/core';
import { Routes } from '@angular/router';
import { RouterModule }        from '@angular/router';
import { SharedModule } from './shared.module';
import { MyComponent } from './MyComponent';
import { MyListComponent }  from './MyListComp';
import { MyListDetailComponent }  from './MyListDetailComp';

const routes: Routes = [
  { path: '',
    component: MyComponent,
    children:   [{ path: 'Home/MyList',    component: MyListComponent },
         { path: 'Home/MyListDetail/:id', component: MyListDetailComponent }]
  }];

@NgModule({
    imports: [RouterModule.forChild([routes]), SharedModule ],
    exports: [RouterModule]})

export class MyListRoutingModule {}

在上面的MyList-routing.module 中,我应该为MyComponent 的路径添加什么?登录方法路由到Home/MyList

编辑

即使在登录点击路径前的斜杠也不起作用。

let link = ['/Home/MyList'];
this.router.navigate(link);

【问题讨论】:

  • 你试过let link = ['/Home/MyList'];(添加/)吗?
  • 是的。那也没有用。我的路线没有使用前面的正斜杠设置。当我将它添加到登录按钮代码时,我也尝试在设置中添加正斜杠
  • 您不能使用前导斜杠设置路线,但如果您不导航到同级或子路线,则必须在导航到路线时添加前导斜杠。
  • 我也试过这种组合。仍然没有工作。
  • 我并不是说这是唯一可能的原因,但从你的问题来看,你有 routerLink 并不明显。是否需要前导斜杠取决于 routerLink 的位置。因此,您可能不需要它。我没有使用其他模块的子路由,因此不知道其他可能的原因。

标签: angular asp.net-core rxjs zone


【解决方案1】:

延迟加载的模块大多是这样的:

 export class default MyModule{

 }

app.routing 中,您最常使用 load Child。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-25
    • 2018-05-03
    • 1970-01-01
    • 1970-01-01
    • 2016-10-08
    相关资源
    最近更新 更多