angularjs - Angular 路由器 3 通配符匹配

标签 angularjs angular

以下路由配置有什么问题?即使有 app/jungle 的路线,我也总是导航到 **

import {bootstrap} from '@angular/platform-browser-dynamic';


import { RouterConfig, provideRouter } from '@angular/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a2d0cdd7d6c7d0e2918c928c928fc0c7d6c38c90" rel="noreferrer noopener nofollow">[email protected]</a>'

import {App} from './app';
import { HomeComponent } from './home.component';
import { JungleComponent } from './jungle.component';
import { NotFoundComponent } from './not-found.component';



const APP_ROUTES: RouterConfig = [
  {
    path: '', pathMatch: '', redirectTo: 'app/home'
  },
  {
    path: 'app/', pathMatch: '', redirectTo: 'app/home'
  },
  {
    path: 'app/home', component: HomeComponent
  },
  {
    path: 'app/jungle', component: JungleComponent
  },
  {
    path: '**', component: NotFoundComponent
  }
]

bootstrap(App, [provideRouter(APP_ROUTES)])
  .catch(err => console.error(err));

Here是个笨蛋。 我正在使用@angular/[email protected]

最佳答案

''pathMatch 的无效值。

pathMatch 支持fullprefix前缀是默认值。

将前两条路由设置为'full':

{
  path: '', pathMatch: 'full', redirectTo: 'app/home'
},

{
  path: 'app/', pathMatch: 'full', redirectTo: 'app/home'
},

{
path: 'app/home', component: HomeComponent
},

{
  path: 'app/jungle', component: JungleComponent
},

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

Plunker example

更新(根据下面的评论)

我不知道为什么尾随 / 使它起作用,但我会使用无组件父路由,例如

const APP_ROUTES: RouterConfig = [
{ path: '', pathMatch: 'full', redirectTo: 'app/home' },
{ path: 'app', children: [
  { path: '', pathMatch: 'full', redirectTo: 'home' },
  { path: 'home', component: HomeComponent },
  { path: 'jungle', component: JungleComponent },
]},
{ path: '**', component: NotFoundComponent }]

Plunker example

关于angularjs - Angular 路由器 3 通配符匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38555005/

相关文章:

javascript - Angular Material - 图像上的元素叠加(在 md-card 中)

javascript - Angular Highcharts 颜色。除了 Highcharts.getOptions().colors[index] 之外还有什么方法可以获取颜色吗?

angular - Storybook 构建失败,出现 TS 错误 ' Interface ' 事件“在 Angular 中错误地扩展了接口(interface) 'KeyboardEvent'”

javascript - AngularJS-ng-repeat 中的项目未在变量更新时更新

angularjs - 仅在点击时触发过滤器

javascript - AngularJS - 动态设置选择默认值的多重选择

javascript - 具有动态输入值的 Angular 形式

javascript - 为什么这个 Angular 2 Pipe 不返回数据?

angular - highcharts-angular 从数据库动态更新

css - 卡住屏幕顶部的 <div>,让剩余的 <div> 填满屏幕并滚动