以下路由配置有什么问题?即使有 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
支持full
和prefix
。 前缀
是默认值。
将前两条路由设置为'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}
]
更新(根据下面的评论)
我不知道为什么尾随 /
使它起作用,但我会使用无组件父路由,例如
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 }]
关于angularjs - Angular 路由器 3 通配符匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38555005/