区分空路径和无参数

时间:2018-02-03 12:18:31

标签: angular angular-router

我想创建一个有两条路线的角度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匹配?

1 个答案:

答案 0 :(得分:2)

使用pathMatch: full空路径路由配置,如下所示:

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