她姓曹 發表於 2019-7-31 15:41:00

Angular配置路由以及动态路由取值传值跳转

<p>Angular配置路由</p>
<p>1、找到 app-routing.module.ts 配置路由</p>
<p>引入组件</p>
<pre class="brush:html;toolbar:false">import&nbsp;{&nbsp;HomeComponent&nbsp;}&nbsp;from&nbsp;'./home/home.component';
import&nbsp;{&nbsp;NewsComponent&nbsp;}&nbsp;from&nbsp;'./news/news.component';
import&nbsp;{&nbsp;NewscontentComponent&nbsp;}&nbsp;from&nbsp;'./newscontent/newscontent.component';</pre>
<p>配置路由</p>
<pre class="brush:html;toolbar:false">const&nbsp;routes:&nbsp;Routes&nbsp;=&nbsp;[
&nbsp;&nbsp;&nbsp;&nbsp;{path:&nbsp;'home',&nbsp;component:&nbsp;HomeComponent},
&nbsp;&nbsp;&nbsp;&nbsp;{path:&nbsp;'news',&nbsp;component:&nbsp;NewsComponent},
&nbsp;&nbsp;&nbsp;&nbsp;{path:&nbsp;'newscontent/:id',&nbsp;component:&nbsp;NewscontentComponent},
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path:&nbsp;'',
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;redirectTo:&nbsp;'/home',
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pathMatch:&nbsp;'full'
&nbsp;&nbsp;&nbsp;&nbsp;}
];</pre>
<p>找到 app.component.html 根组件模板,配置 router-outlet 显示动态加载的路由</p>
<pre class="brush:html;toolbar:false">&lt;h1&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;a&nbsp;routerLink="/home"&gt;首页&lt;/a&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;a&nbsp;routerLink="/news"&gt;新闻&lt;/a&gt;
&lt;/h1&gt;
&lt;router-outlet&gt;&lt;/router-outlet&gt;</pre>
<p>&nbsp;</p>
<p>Angular routerLinkActive设置routerLink 默认选中路由</p>
<pre class="brush:html;toolbar:false">&lt;h1&gt;
&lt;a&nbsp;="[&nbsp;'/home'&nbsp;]"&nbsp;routerLinkActive="active"&gt;首页&lt;/a&gt;
&lt;a&nbsp;="[&nbsp;'/news'&nbsp;]"&nbsp;routerLinkActive="active"&gt;新闻&lt;/a&gt;
&lt;/h1&gt;</pre>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>动态路由传值与取值</p>
<p>跳转传值</p>
<pre class="brush:html;toolbar:false">&lt;a&nbsp;="[&nbsp;'/newscontent/',aid]"&gt;跳转到详情&lt;/a&gt;
&lt;a&nbsp;routerLink="/newscontent/{{aid}}"&gt;跳转到详情&lt;/a&gt;</pre>
<p>获取动态路由的值</p>
<pre class="brush:c#;toolbar:false">import&nbsp;{&nbsp;ActivatedRoute}&nbsp;from&nbsp;'@angular/router';

constructor(&nbsp;private&nbsp;route:&nbsp;ActivatedRoute)&nbsp;{
}
public&nbsp;id:&nbsp;Number;
ngOnInit()&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;console.log(this.route.params);
&nbsp;&nbsp;&nbsp;&nbsp;this.route.params.subscribe(data=&gt;this.id=data.id);
}</pre>
<p>&nbsp;</p>
<p>动态路由的 js 跳转</p>
<pre class="brush:html;toolbar:false">import&nbsp;{&nbsp;Router&nbsp;}&nbsp;from&nbsp;'@angular/router

export&nbsp;class&nbsp;HomeComponent&nbsp;implements&nbsp;OnInit&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;constructor(private&nbsp;router:&nbsp;Router)&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ngOnInit()&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;goNews(){
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;this.router.navigate(['/newscontent',&nbsp;hero.id]);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.router.navigate(['/newscontent']);
&nbsp;&nbsp;&nbsp;&nbsp;}
}</pre><br><br>
来源:https://www.cnblogs.com/reaf/p/11276744.html
頁: [1]
查看完整版本: Angular配置路由以及动态路由取值传值跳转