首页 文章

Angular 2无法匹配Route错误

提问于
浏览
1

我得到角度路线错误

未捕获(承诺):错误:无法匹配任何路线 . URL段:'null'错误:无法匹配任何路由 . URL Segment:'null'at ApplyRedirects.webpackJsonp ... / .. / .. / router / @ angular / rroure.es5.js.ApplyRedirects.noMatchError

下面是我的路线对象:

export const ROUTES: Routes = [
  { path: '', component: TrendsComponent},
  { path: 'trends', component: TrendsComponent }
];

我正在使用@ angular / router 4.0.0

1 回答

  • 1

    尝试按以下方式设置路由,使用 redirectTo 在启动时将用户路由到路径"/trends" .

    确保将RouterModule导入 NgModule . 对于根模块或类似模块,您可以使用 RouterModule.forRoot(ROUTES) ,对于要使用 RouterModule.forChild(ROUTES) 的要素模块 . 确保在根和任何功能模块中都包含 <router-outlet></router-outlet> .

    const ROUTES: Routes = [
      { path: 'trends', component: TrendsComponent },
      { path: '', redirectTo: '/trends', pathMatch: 'full' }
    ];
    
    @Component({
      selector: 'my-app',
      template: `
        <div>
          <h2>Hello World</h2>
          <p>Redirect to path /trends component is occurring on startup</p>
          <router-outlet></router-outlet>
        </div>
      `,
    })
    export class App {}
    
    @NgModule({
      imports: [ BrowserModule, RouterModule.forRoot(ROUTES) ],
      declarations: [ App, TrendsComponent ],
      bootstrap: [ App ]
    })
    export class AppModule {}
    

    这是一个展示功能的plunker .

    Updated: plunker已更新为显示声明和导入子路由和模块(非延迟加载) .

    希望这会有所帮助 .

相关问题