瓦瓦枫 發表於 2019-9-5 17:49:00

angular 路由跳转以及传参

<p><span style="color: rgba(0, 0, 255, 1); text-decoration: underline"><em><span style="font-size: 18px"><strong>1. 路由跳转方式一: /路由?id='001' 方式 -- queryParams 方式</strong></span></em></span></p>
<p><span style="font-size: 16px"><strong> 路由配置:{ path: <span style="color: rgba(154, 168, 58, 1)">'details'<span style="color: rgba(197, 200, 198, 1)">, component: <span style="color: rgba(96, 137, 180, 1)">bookDetailsComponent<span style="color: rgba(197, 200, 198, 1)"> }</span></span></span></span></strong></span></p>
<p><span style="font-size: 16px"><strong>&nbsp;&nbsp; a.&nbsp; 指令跳转:</strong></span></p>
<div style="color: rgba(197, 200, 198, 1); line-height: 19px; font-family: Consolas, &quot;Courier New&quot;, monospace; font-size: 14px; font-weight: normal; white-space: pre; background-color: rgba(30, 30, 30, 1)">
<div><span style="color: rgba(197, 200, 198, 1)">   <span style="color: rgba(96, 137, 180, 1)">&lt;a<span style="color: rgba(208, 179, 68, 1)"> =<span style="color: rgba(154, 168, 58, 1)">"['/details']"<span style="color: rgba(208, 179, 68, 1)"> =<span style="color: rgba(154, 168, 58, 1)">"{id: item.id}"<span style="color: rgba(208, 179, 68, 1)"> style=<span style="color: rgba(154, 168, 58, 1)">"<span style="color: rgba(154, 168, 58, 1)">color:blue; font-size: 12px;cursor:pointer"<span style="color: rgba(96, 137, 180, 1)">&gt;<span style="color: rgba(197, 200, 198, 1)">查看详情<span style="color: rgba(96, 137, 180, 1)">&lt;/a&gt;</span></span></span></span></span></span></span></span></span></span></span></span></div>
</div>
<p><span style="font-size: 16px"><strong>  routerLink: 跳转的路由,数组形式,传参有两种写法: 1. 使用 =<span style="color: rgba(154, 168, 58, 1)">"{id: item.id}", 2. =<span style="color: rgba(154, 168, 58, 1)">"['/details', id]", 数组第一个值是            路由,第二个值是要传递的参数</span></span></strong></span></p>
<p><span style="font-size: 16px"><strong><span style="color: rgba(154, 168, 58, 1)"><span style="color: rgba(154, 168, 58, 1)">&nbsp;</span></span></strong><strong><span style="color: rgba(0, 0, 0, 1)"> b. js 实现跳转:</span></strong></span></p>
<p><span style="font-size: 16px"><strong><span style="color: rgba(154, 168, 58, 1)"><span style="color: rgba(154, 168, 58, 1)"><span style="color: rgba(0, 0, 0, 1)">  </span></span></span></strong></span></p>
<p><span style="font-size: 16px"><strong><span style="color: rgba(154, 168, 58, 1)"><span style="color: rgba(154, 168, 58, 1)"><span style="color: rgba(0, 0, 0, 1)">其中 this.router 是 Router 的实例</span></span></span></strong></span></p>
<p><span style="font-size: 16px"><strong><span style="color: rgba(154, 168, 58, 1)"><span style="color: rgba(154, 168, 58, 1)"><span style="color: rgba(0, 0, 0, 1)">import&nbsp; { Router&nbsp;} from '@angular/router'</span></span></span></strong></span></p>
<div style="color: rgba(197, 200, 198, 1); line-height: 19px; font-family: Consolas, &quot;Courier New&quot;, monospace; font-size: 14px; font-weight: normal; white-space: pre; background-color: rgba(30, 30, 30, 1)">
<div><span style="color: rgba(197, 200, 198, 1)"> <span style="color: rgba(152, 114, 162, 1)">constructor<span style="color: rgba(197, 200, 198, 1)">(<span style="color: rgba(152, 114, 162, 1)">private<span style="color: rgba(197, 200, 198, 1)"> <span style="color: rgba(96, 137, 180, 1)">router<span style="color: rgba(103, 104, 103, 1)">:<span style="color: rgba(197, 200, 198, 1)"> <span style="color: rgba(155, 0, 0, 1)">Router<span style="color: rgba(197, 200, 198, 1)">) {</span></span></span></span></span></span></span></span></span></span></div>
<div><span style="color: rgba(197, 200, 198, 1)">&nbsp;}</span></div>
<div><span style="color: rgba(197, 200, 198, 1)">&nbsp;<span style="color: rgba(206, 103, 0, 1)">jumpDetial<span style="color: rgba(197, 200, 198, 1)">(<span style="color: rgba(96, 137, 180, 1)">bookId<span style="color: rgba(103, 104, 103, 1)">:<span style="color: rgba(197, 200, 198, 1)"> <span style="color: rgba(199, 68, 74, 1)">string<span style="color: rgba(197, 200, 198, 1)">)<span style="color: rgba(103, 104, 103, 1)">:<span style="color: rgba(197, 200, 198, 1)"> <span style="color: rgba(199, 68, 74, 1)">void<span style="color: rgba(197, 200, 198, 1)"> {</span></span></span></span></span></span></span></span></span></span></span></span></div>
<div><span style="color: rgba(197, 200, 198, 1)">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgba(199, 68, 74, 1)">this<span style="color: rgba(197, 200, 198, 1)">.<span style="color: rgba(96, 137, 180, 1)">router<span style="color: rgba(197, 200, 198, 1)">.<span style="color: rgba(206, 103, 0, 1)">navigate<span style="color: rgba(197, 200, 198, 1)">([<span style="color: rgba(154, 168, 58, 1)">'/details'<span style="color: rgba(197, 200, 198, 1)">], {</span></span></span></span></span></span></span></span></span></div>
<div><span style="color: rgba(197, 200, 198, 1)">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; queryParams: {</span></div>
<div><span style="color: rgba(197, 200, 198, 1)">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; id: <span style="color: rgba(96, 137, 180, 1)">bookId</span></span></div>
<div><span style="color: rgba(197, 200, 198, 1)">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></div>
<div><span style="color: rgba(197, 200, 198, 1)">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; })</span></div>
<div><span style="color: rgba(197, 200, 198, 1)">&nbsp;&nbsp;&nbsp; }</span></div>
</div>
<p><span style="font-size: 14px"><span style="color: rgba(154, 168, 58, 1)"><span style="color: rgba(154, 168, 58, 1)"><span style="color: rgba(0, 0, 0, 1)">this.router.navigate(['user', 1]); 以根路由为起点跳转</span></span></span></span></p>
<p>this.router.navigate(['user', 1],{<strong>relativeTo</strong>: route}); 默认值为根路由,设置后相对当前路由跳转,跳转到子路由</p>
<p>this.router.navigate(['user', 1],{ queryParams: { id: 1 } }); 路由中传参数 /user/1?id=1</p>
<p>this.router.navigate(['view', 1], { preserveQueryParams: true }); 默认值为false,设为true,保留之前路由中的查询参数/user?id=1 to /view?id=1</p>
<p>this.router.navigate(['user', 1],{ fragment: 'top' }); 路由中锚点跳转 /user/1#top</p>
<p>this.router.navigate(['/view'], { preserveFragment: true }); 默认值为false,设为true,保留之前路由中的锚点/user/1<strong>#top</strong> to /view<strong>#top</strong></p>
<p>this.router.navigate(['/user',1], { <strong>skipLocationChange</strong>: true }); 默认值为false,设为true路由跳转时浏览器中的url会保持不变,但是传入的参数依然有效</p>
<p>this.router.navigate(['/user',1], { replaceUrl: true }); 未设置时默认为true,设置为false路由不会进行跳转</p>
<p><span style="color: rgba(0, 0, 255, 1); text-decoration: underline"><em><span style="font-size: 18px"><strong><span style="text-decoration: underline">2. 以 <span style="text-decoration: underline">/路由/参数</span>&nbsp; 动态路由的方式跳转 -- snapshot方式</span></strong></span></em></span></p>
<p>&nbsp;<span style="color: rgba(0, 0, 0, 1); font-size: 16px"><strong>路由配置:&nbsp; { path: 'details/:id', component: bookDetailsComponent }</strong></span></p>
<p><span style="color: rgba(0, 0, 0, 1)">1. 指令跳转传参:</span></p>
<p><span style="color: rgba(197, 200, 198, 1)"><span style="color: rgba(154, 168, 58, 1)"><span style="color: rgba(197, 200, 198, 1)"><span style="color: rgba(96, 137, 180, 1)"><span style="color: rgba(197, 200, 198, 1)">  </span></span></span></span></span></p>
<div style="color: rgba(197, 200, 198, 1); line-height: 19px; font-family: Consolas, &quot;Courier New&quot;, monospace; font-size: 14px; font-weight: normal; white-space: pre; background-color: rgba(30, 30, 30, 1)">
<div><span style="color: rgba(96, 137, 180, 1)">  &lt;a<span style="color: rgba(208, 179, 68, 1)"> =<span style="color: rgba(154, 168, 58, 1)">"['/details', item.id]"&lt;/a&gt;</span></span></span></div>
</div>
<p><span style="color: rgba(0, 0, 0, 1)">2. js 跳转:</span></p>
<p><span style="color: rgba(197, 200, 198, 1)"><span style="color: rgba(154, 168, 58, 1)"><span style="color: rgba(197, 200, 198, 1)"><span style="color: rgba(96, 137, 180, 1)"><span style="color: rgba(197, 200, 198, 1)">  </span></span></span></span></span></p>
<div style="color: rgba(197, 200, 198, 1); line-height: 19px; font-family: Consolas, &quot;Courier New&quot;, monospace; font-size: 14px; font-weight: normal; white-space: pre; background-color: rgba(30, 30, 30, 1)">
<div><span style="color: rgba(199, 68, 74, 1)">  this<span style="color: rgba(197, 200, 198, 1)">.<span style="color: rgba(96, 137, 180, 1)">router<span style="color: rgba(197, 200, 198, 1)">.<span style="color: rgba(206, 103, 0, 1)">navigate<span style="color: rgba(197, 200, 198, 1)">([<span style="color: rgba(154, 168, 58, 1)">'/details'<span style="color: rgba(197, 200, 198, 1)">, <span style="color: rgba(154, 168, 58, 1)">'1'<span style="color: rgba(197, 200, 198, 1)">]</span></span></span></span></span></span></span></span></span></span></div>
</div>
<p>&nbsp;</p>
<p><span style="color: rgba(0, 0, 0, 1); font-size: 16px"><strong>3. 获取参数</strong></span></p>
<p><span style="color: rgba(0, 0, 0, 1); font-size: 16px"><strong>this.route 是 ActivatedRoute 的实例</strong></span></p>
<div style="color: rgba(197, 200, 198, 1); line-height: 19px; font-family: Consolas, &quot;Courier New&quot;, monospace; font-size: 14px; font-weight: normal; white-space: pre; background-color: rgba(30, 30, 30, 1)">
<div><span style="color: rgba(152, 114, 162, 1)">import<span style="color: rgba(197, 200, 198, 1)">&nbsp;{&nbsp;<span style="color: rgba(96, 137, 180, 1)">ActivatedRoute<span style="color: rgba(197, 200, 198, 1)">&nbsp;}&nbsp;<span style="color: rgba(152, 114, 162, 1)">from<span style="color: rgba(197, 200, 198, 1)">&nbsp;<span style="color: rgba(154, 168, 58, 1)">'@angular/router'</span></span></span></span></span></span></span></div>
</div>
<p><span style="color: rgba(0, 0, 0, 1); font-size: 16px"><strong>a. 快照方式获取参数 snapshot</strong></span></p>
<div style="color: rgba(197, 200, 198, 1); line-height: 19px; font-family: Consolas, &quot;Courier New&quot;, monospace; font-size: 14px; font-weight: normal; white-space: pre; background-color: rgba(30, 30, 30, 1)">
<div><span style="color: rgba(199, 68, 74, 1)">// 方式1</span></div>
<div><span style="color: rgba(199, 68, 74, 1)">this<span style="color: rgba(197, 200, 198, 1)">.<span style="color: rgba(152, 114, 162, 1)">queryParams<span style="color: rgba(197, 200, 198, 1)"> <span style="color: rgba(103, 104, 103, 1)">=<span style="color: rgba(197, 200, 198, 1)"> <span style="color: rgba(199, 68, 74, 1)">this<span style="color: rgba(197, 200, 198, 1)">.<span style="color: rgba(96, 137, 180, 1)">route<span style="color: rgba(197, 200, 198, 1)">.<span style="color: rgba(96, 137, 180, 1)">snapshot<span style="color: rgba(197, 200, 198, 1)">.<span style="color: rgba(152, 114, 162, 1)">params<span style="color: rgba(197, 200, 198, 1)">[<span style="color: rgba(154, 168, 58, 1)">'id'<span style="color: rgba(197, 200, 198, 1)">]</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></div>
<div><span style="color: rgba(199, 68, 74, 1)"><span style="color: rgba(197, 200, 198, 1)"><span style="color: rgba(152, 114, 162, 1)"><span style="color: rgba(197, 200, 198, 1)"><span style="color: rgba(103, 104, 103, 1)"><span style="color: rgba(197, 200, 198, 1)"><span style="color: rgba(199, 68, 74, 1)"><span style="color: rgba(197, 200, 198, 1)"><span style="color: rgba(96, 137, 180, 1)"><span style="color: rgba(197, 200, 198, 1)"><span style="color: rgba(96, 137, 180, 1)"><span style="color: rgba(197, 200, 198, 1)"><span style="color: rgba(152, 114, 162, 1)"><span style="color: rgba(197, 200, 198, 1)"><span style="color: rgba(154, 168, 58, 1)"><span style="color: rgba(197, 200, 198, 1)">// 方式2</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></div>
<div>
<div><span style="color: rgba(199, 68, 74, 1)">this<span style="color: rgba(197, 200, 198, 1)">.<span style="color: rgba(96, 137, 180, 1)">route<span style="color: rgba(197, 200, 198, 1)">.p<span style="color: rgba(96, 137, 180, 1)">arams<span style="color: rgba(197, 200, 198, 1)">.<span style="color: rgba(206, 103, 0, 1)">subscribe<span style="color: rgba(197, 200, 198, 1)">(<span style="color: rgba(96, 137, 180, 1)">params<span style="color: rgba(152, 114, 162, 1)">=&gt;<span style="color: rgba(197, 200, 198, 1)">{</span></span></span></span></span></span></span></span></span></span></span></div>
<div><span style="color: rgba(197, 200, 198, 1)"><span style="color: rgba(199, 68, 74, 1)">  this<span style="color: rgba(197, 200, 198, 1)">.<span style="color: rgba(152, 114, 162, 1)">queryParams<span style="color: rgba(197, 200, 198, 1)"> <span style="color: rgba(103, 104, 103, 1)">=<span style="color: rgba(197, 200, 198, 1)"> <span style="color: rgba(96, 137, 180, 1)">params</span></span></span></span></span></span></span></span></div>
<div><span style="color: rgba(197, 200, 198, 1)">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: rgba(199, 68, 74, 1)">console<span style="color: rgba(197, 200, 198, 1)">.<span style="color: rgba(152, 114, 162, 1)">log<span style="color: rgba(197, 200, 198, 1)">(<span style="color: rgba(199, 68, 74, 1)">this<span style="color: rgba(197, 200, 198, 1)">.<span style="color: rgba(152, 114, 162, 1)">queryParams<span style="color: rgba(197, 200, 198, 1)">)</span></span></span></span></span></span></span></span></span></div>
<div><span style="color: rgba(197, 200, 198, 1)">})</span></div>
</div>
</div>
<p><span style="color: rgba(0, 0, 0, 1); font-size: 16px"><strong>b. queryParams 方式获取参数</strong></span></p>
<div style="color: rgba(197, 200, 198, 1); line-height: 19px; font-family: Consolas, &quot;Courier New&quot;, monospace; font-size: 14px; font-weight: normal; white-space: pre; background-color: rgba(30, 30, 30, 1)">
<div><span style="color: rgba(199, 68, 74, 1)">this<span style="color: rgba(197, 200, 198, 1)">.<span style="color: rgba(96, 137, 180, 1)">route<span style="color: rgba(197, 200, 198, 1)">.<span style="color: rgba(96, 137, 180, 1)">queryParams<span style="color: rgba(197, 200, 198, 1)">.<span style="color: rgba(206, 103, 0, 1)">subscribe<span style="color: rgba(197, 200, 198, 1)">(<span style="color: rgba(96, 137, 180, 1)">params<span style="color: rgba(152, 114, 162, 1)">=&gt;<span style="color: rgba(197, 200, 198, 1)">{</span></span></span></span></span></span></span></span></span></span></span></div>
<div><span style="color: rgba(197, 200, 198, 1)"><span style="color: rgba(199, 68, 74, 1)">  this<span style="color: rgba(197, 200, 198, 1)">.<span style="color: rgba(152, 114, 162, 1)">queryParams<span style="color: rgba(197, 200, 198, 1)"> <span style="color: rgba(103, 104, 103, 1)">=<span style="color: rgba(197, 200, 198, 1)"> <span style="color: rgba(96, 137, 180, 1)">params<span style="color: rgba(197, 200, 198, 1)">[<span style="color: rgba(154, 168, 58, 1)">'id'<span style="color: rgba(197, 200, 198, 1)">]</span></span></span></span></span></span></span></span></span></span></span></div>
<div><span style="color: rgba(197, 200, 198, 1)">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: rgba(199, 68, 74, 1)">console<span style="color: rgba(197, 200, 198, 1)">.<span style="color: rgba(152, 114, 162, 1)">log<span style="color: rgba(197, 200, 198, 1)">(<span style="color: rgba(199, 68, 74, 1)">this<span style="color: rgba(197, 200, 198, 1)">.<span style="color: rgba(152, 114, 162, 1)">queryParams<span style="color: rgba(197, 200, 198, 1)">)</span></span></span></span></span></span></span></span></span></div>
<div><span style="color: rgba(197, 200, 198, 1)">})</span></div>
</div>
<p>&nbsp;</p>
<hr>
<p>&nbsp;</p>
<p>路由跳转时遇到的问题:</p>
<p>  1. 组件中有 跳转, 父元素有点击事件。 点击 routerLink 元素会触发父元素事件 的问题?</p>
<p>    解决: 在routerLink元素上添加 click 事件,组织事件冒泡。</p>
<p>    代码:</p>
<div style="color: rgba(197, 200, 198, 1); line-height: 19px; font-family: Consolas, &quot;Courier New&quot;, monospace; font-size: 14px; font-weight: normal; white-space: pre; background-color: rgba(30, 30, 30, 1)">
<div><span style="color: rgba(96, 137, 180, 1)">&lt;ul<span style="color: rgba(208, 179, 68, 1)"> style=<span style="color: rgba(154, 168, 58, 1)">"<span style="color: rgba(208, 132, 66, 1)">display: flex;"<span style="color: rgba(96, 137, 180, 1)">&gt;</span></span></span></span></span></div>
<div><span style="color: rgba(197, 200, 198, 1)">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgba(96, 137, 180, 1)">&lt;li<span style="color: rgba(208, 179, 68, 1)"> *ngFor=<span style="color: rgba(154, 168, 58, 1)">"let item of bookList"<span style="color: rgba(208, 179, 68, 1)"> style=<span style="color: rgba(154, 168, 58, 1)">"<span style="color: rgba(208, 132, 66, 1)">width: 300px; height: 320px; border: 1px solid #ddd;margin-left: 20px;"</span></span></span></span></span></span></span></div>
<div><span style="color: rgba(208, 179, 68, 1)">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (click)=<span style="color: rgba(154, 168, 58, 1)">"$event.stopPropagation();$event.preventDefault(); jumpDetial(item.id)"<span style="color: rgba(96, 137, 180, 1)">&gt;</span></span></span></div>
<div><span style="color: rgba(197, 200, 198, 1)">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgba(96, 137, 180, 1)">&lt;div&gt;<span style="color: rgba(197, 200, 198, 1)">{{ item.name }}<span style="color: rgba(96, 137, 180, 1)">&lt;/div&gt;</span></span></span></span></div>
<div><span style="color: rgba(197, 200, 198, 1)">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgba(96, 137, 180, 1)">&lt;div&gt;<span style="color: rgba(197, 200, 198, 1)">{{ item.id }}<span style="color: rgba(96, 137, 180, 1)">&lt;/div&gt;</span></span></span></span></div>
<div><span style="color: rgba(197, 200, 198, 1)">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgba(96, 137, 180, 1)">&lt;a<span style="color: rgba(208, 179, 68, 1)"> (click)=<span style="color: rgba(154, 168, 58, 1)">"$event.stopPropagation()"<span style="color: rgba(208, 179, 68, 1)"> =<span style="color: rgba(154, 168, 58, 1)">"['/details', '1']"<span style="color: rgba(208, 179, 68, 1)"> =<span style="color: rgba(154, 168, 58, 1)">"{id: item.id}"<span style="color: rgba(208, 179, 68, 1)">&nbsp; style=<span style="color: rgba(154, 168, 58, 1)">"<span style="color: rgba(154, 168, 58, 1)">color:blue; font-size: 12px;cursor:pointer"<span style="color: rgba(96, 137, 180, 1)">&gt;<span style="color: rgba(197, 200, 198, 1)">查看详情<span style="color: rgba(96, 137, 180, 1)">&lt;/a&gt;</span></span></span></span></span></span></span></span></span></span></span></span></span></span></div>
<div><span style="color: rgba(197, 200, 198, 1)">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgba(96, 137, 180, 1)">&lt;br&gt;</span></span></div>
<div><span style="color: rgba(197, 200, 198, 1)">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgba(96, 137, 180, 1)">&lt;a<span style="color: rgba(208, 179, 68, 1)"> (click)=<span style="color: rgba(154, 168, 58, 1)">"$event.stopPropagation()"<span style="color: rgba(208, 179, 68, 1)"> =<span style="color: rgba(154, 168, 58, 1)">"['./intro']"<span style="color: rgba(96, 137, 180, 1)">&gt;&lt;i&gt;<span style="color: rgba(197, 200, 198, 1)">简介<span style="color: rgba(96, 137, 180, 1)">&lt;/i&gt;&lt;/a&gt;</span></span></span></span></span></span></span></span></span></div>
<div><span style="color: rgba(197, 200, 198, 1)">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgba(96, 137, 180, 1)">&lt;/li&gt;</span></span></div>
<div><span style="color: rgba(197, 200, 198, 1)">&nbsp;&nbsp; <span style="color: rgba(96, 137, 180, 1)">&lt;/ul&gt;</span></span></div>
</div>
<p>   2. 事件跳转时,如果该组件没有子路由, = "home" 可以跳转成功, 如果该组件有子路由 不加&nbsp; " / " 则认为时跳转到&nbsp; "/父路由/home"</p>
<p>   所以,路由跳转 = "/home" 表示一级路由&nbsp; = "./home" 表示二级路由</p>
<p>  </p>
<p>  3. 跳转到子路由</p>
<p>  </p>
<div style="color: rgba(197, 200, 198, 1); line-height: 19px; font-family: Consolas, &quot;Courier New&quot;, monospace; font-size: 14px; font-weight: normal; white-space: pre; background-color: rgba(30, 30, 30, 1)">
<div><span style="color: rgba(197, 200, 198, 1)"> <span style="color: rgba(206, 103, 0, 1)">toChildPath<span style="color: rgba(197, 200, 198, 1)">()<span style="color: rgba(103, 104, 103, 1)">:<span style="color: rgba(197, 200, 198, 1)"> <span style="color: rgba(199, 68, 74, 1)">void<span style="color: rgba(197, 200, 198, 1)"> {</span></span></span></span></span></span></span></div>
<div><span style="color: rgba(197, 200, 198, 1)">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgba(154, 155, 153, 1)">// 方式1: 全路径匹配</span></span></div>
<div><span style="color: rgba(197, 200, 198, 1)">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgba(199, 68, 74, 1)">this<span style="color: rgba(197, 200, 198, 1)">.<span style="color: rgba(96, 137, 180, 1)">router<span style="color: rgba(197, 200, 198, 1)">.<span style="color: rgba(206, 103, 0, 1)">navigate<span style="color: rgba(197, 200, 198, 1)">([<span style="color: rgba(154, 168, 58, 1)">'/book/intro'<span style="color: rgba(197, 200, 198, 1)">]) </span></span></span></span></span></span></span></span></span></div>
<div><span style="color: rgba(197, 200, 198, 1)">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgba(154, 155, 153, 1)">// 方式2: 使用relativeTo. 其中 router 是Router的实例,this.route是 ActivatedRoute 的实例</span></span></div>
<div><span style="color: rgba(197, 200, 198, 1)">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgba(199, 68, 74, 1)">this<span style="color: rgba(197, 200, 198, 1)">.<span style="color: rgba(96, 137, 180, 1)">router<span style="color: rgba(197, 200, 198, 1)">.<span style="color: rgba(206, 103, 0, 1)">navigate<span style="color: rgba(197, 200, 198, 1)">([<span style="color: rgba(154, 168, 58, 1)">'intro'<span style="color: rgba(197, 200, 198, 1)">], {</span></span></span></span></span></span></span></span></span></div>
<div><span style="color: rgba(197, 200, 198, 1)">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; relativeTo: <span style="color: rgba(199, 68, 74, 1)">this<span style="color: rgba(197, 200, 198, 1)">.<span style="color: rgba(152, 114, 162, 1)">route</span></span></span></span></div>
<div><span style="color: rgba(197, 200, 198, 1)">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; })</span></div>
<div><span style="color: rgba(197, 200, 198, 1)">&nbsp;&nbsp;&nbsp; }</span></div>
</div>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<hr>
<p>&nbsp;</p>
<p><span style="font-size: 18px"><strong>&nbsp;选中路由高亮:</strong></span></p>
<div style="color: rgba(197, 200, 198, 1); line-height: 19px; font-family: Consolas, &quot;Courier New&quot;, monospace; font-size: 14px; font-weight: normal; white-space: pre; background-color: rgba(30, 30, 30, 1)">
<div><span style="color: rgba(96, 137, 180, 1)">&lt;li<span style="color: rgba(208, 179, 68, 1)">&nbsp;class=<span style="color: rgba(154, 168, 58, 1)">"nav_item"<span style="color: rgba(208, 179, 68, 1)">&nbsp;=<span style="color: rgba(154, 168, 58, 1)">"['/home']"<span style="color: rgba(208, 179, 68, 1)">&nbsp;routerLinkActive=<span style="color: rgba(154, 168, 58, 1)">"active"<span style="color: rgba(96, 137, 180, 1)">&gt;<span style="color: rgba(197, 200, 198, 1)">首页<span style="color: rgba(96, 137, 180, 1)">&lt;/li&gt;</span></span></span></span></span></span></span></span></span></span></div>
</div>
<p>&nbsp;routerLinkActive 属性等于一个样式类名</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>  </p><br><br>
来源:https://www.cnblogs.com/monkey-K/p/11455272.html
頁: [1]
查看完整版本: angular 路由跳转以及传参