/** |
来中文
- routerLink 接受的是一个数组, 希望是得到一个 路由名字 参数 子路由名字 子路由参数
说到子路由
上例子
根路由配置
<li (click)="loopNav($event)"> <a [routerLink]="['ParentApp','NumberList']">ParentApp</a></li> |
子组件中子路由配置
<li> <a [routerLink]="['./NumberList']">NumberList</a></li> |
父路由中的 ‘…’ 是根据报错信息加上的。具体原因要看源码中哪里有用到
来自官网:
Notice that the path ends with a slash and three trailing periods (/…).
That means this is an incomplete route (a non-terminal route). The finished route will be some combination of the parent /crisis-center/ route and a route from the child router that belongs to the designated component.
https://github.com/dreambo8563/node_angular2/blob/ng-pages/app/components/CompLoaderSample.ts
源码
@Directive({ |
看能延伸出什么其他的东西:
- annotation
@Directive({
selector: '[routerLink]',
inputs: ['routeParams: routerLink', 'target: target'],
host: {
'(click)': 'onClick()',
'[attr.href]': 'visibleHref',
'[class.router-link-active]': 'isRouteActive'
}
})
值得一说的就是 inputs- routeParams 接受上面说的数组参数。(路由子路由 参数什么)
target - 来自w3c school
- _blank 在新窗口中打开被链接文档。
- _self 默认。在相同的框架中打开被链接文档。
- _parent 在父框架集中打开被链接文档。
- _top 在整个窗口中打开被链接文档。
- framename 在指定的框架中打开被链接文档。
visibleHref: string; |
当路由link需要当做anchor 的时候需要 href 属性
”aux routes“ 这个后面应该有单独的class 说
this._navigationInstruction = this._router.generate(this._routeParams); |
根据inputs 产生 Instruction
转换成url 因为有子路由神马的,参数的
根据你的策略生成外面看到的url
下面是 prepareExternalUrl 的作用
/**
* Given a string representing a URL, returns the platform-specific external URL path.
* If the given URL doesn't begin with a leading slash (`'/'`), this method adds one
* before normalizing. This method will also add a hash if `HashLocationStrategy` is
* used, or the `APP_BASE_HREF` if the `PathLocationStrategy` is in use.
*/
get isRouteActive(): boolean { return this._router.isRouteActive(this._navigationInstruction); } |
从这句可知,想要知道哪个路由是是否active 可以
- 把当前路由参数变成 Instruction
- 在根据this._router.isRouteActive 方法来判断
this._router.navigateByInstruction(this._navigationInstruction); |
onclick中代码可以学到 ,当你得到某个 instruction的时候,你可以用代码跳转
this._router.navigateByInstruction
Instruction 的简单介绍
* `Instruction` is a tree of {@link ComponentInstruction}s with all the information needed
* to transition each component in the app to a given route, including all auxiliary routes.
*
* `Instruction`s can be created using {@link Router#generate}, and can be used to
* perform route changes with {@link Router#navigateByInstruction}.