Angular2的内置指令可分为通用指令、路由指令、表单指令
路由指令有3个,包含在RouterModule中 :
- RouterLink
- RouterOutlet
- RouterLinkActive
RouterLink : 使得应用可以链接到特定组件,可以被应用在任何HTML元素上。
<a [routerLink]="['/user/jim']">Jim</a>
- If the first segment begins with
/
, the router will look up the route from the root of the app. - If the first segment begins with
./
, or doesn't begin with a slash, the router will instead look in the children of the current activated route. - And if the first segment begins with
../
, the router will go up one level.
RouterOutlet : 路由跳转时,Angular会查找当前匹配的组件并将组件插入到RouterOutlet中。
<router-outlet></router-outlet>
RouterLinkActive : 当路由激活时为当前该元素添加指定CSS类样式 。
<a [routerLink]="['/user/bob']" routerLinkActive="active-link">Bob</a>
//多个CSS类时
<a [routerLink]="['/user/bob']" [routerLinkActive]="['class1', 'class2']">Bob</a>