区分空路径和无参数

问题描述 投票:0回答:1

我想创建一个有两条路线的角度SPA:

http://mydomain/       <- Shows homepage
http://mydomain/2017   <- Shows results for 2017
http://mydomain/2018   <- Shows results for 2018

其中URL的20172018部分是控制器的参数。我试图使用以下路线实现此目的:

export const MyAppRoutes: Routes = [ 
    { path: "",  component: HomepageComponent },
    { path: ":resultId",  component: ResultComponent },
];

但是,访问http://mydomain将呈现ResultComponent;路线参数只是空的。

如果参数ResultComponent存在,我如何通知路由器它应该只与:resultId匹配?

angular angular-router
1个回答
2
投票

使用pathMatch: full与空路径路由配置如下:

export const MyAppRoutes: Routes = [ 
    { path: "",  component: HomepageComponent, pathMatch: full },
    { path: ":resultId",  component: ResultComponent },
];
© www.soinside.com 2019 - 2024. All rights reserved.