Article

Github

Auxiliary Routes is is little bit hard to understand.

Here is demo, link

You can see each category has an own 'View detials' button, and there is a side menu on the right side.

What we want is when we click the "View details" button, it will nav to category detail component along with the id (in this case is the title ex: 'development').

Also we want the side menu also get the information what button have been clicked.

Vice Versa....

Once we got those id, then we can do the rest application logic, which won't be included here... but the importantce is to get the id for both category detail component and sidemenu component.

The router config:

export const routerConfig: Routes = [
{
path: 'home',
component: HomeComponent
},
{
path: 'courses',
component: CoursesComponent,
children: [
{
path: '',
component: CourseCardsComponent
},
{
path: ':id',
component: CoursesCategoryComponent
},
{
path: '',
outlet: 'sidemenu',
component: SideMenuComponent
},
{
path: ':id',
outlet: 'sidemenu',
component: SideMenuComponent
}
]
},
{
path: '',
redirectTo: '/home',
pathMatch: 'full'
},
{
path: '**',
redirectTo: '/home',
pathMatch: 'full'
}
];

Look at 'courses' router, we have two empty paths, it means for the empty path, we have tow componet to display,  one is 'CourseCardsComponent' and 'SidemenuComponent'. 'CourseCardsComponent' will be displayed in primay outlet, and 'SidemenuComponent' will be display in auxiliary router outlet.

<main>
<div class="container">
<div class="row row-offcanvas row-offcanvas-right">
<div class="col-xs-12 col-sm-9">
<ol class="breadcrumb">
<li class="breadcrumb-item"><a routerLink="/home">Home</a></li>
<li class="breadcrumb-item active">Courses</li>
</ol>
<div class="jumbotron">
<h1>Course Categories!</h1>
<p>This is a list of course categories, click on each to see a list of courses for a given
category.</p>
</div>
<router-outlet></router-outlet>
</div>
<router-outlet name="sidemenu"></router-outlet>
</div>
</div>
</main>

So now when we click the button, how to make url like this:

/courses/(development//sidemenu:development)

and what does it means?

This url means:

  • the courses url segment is active

  • inside it the primary route is set to /courses/development

  • the auxiliary child route 'development' is active for the outlet sidemenu

So there are two thing going on, for the primay outlet, we are in /courses/development state, and for sidemenu, we are also in /development state.

import { Component, OnInit } from '@angular/core';
import {Router, ActivatedRoute} from "@angular/router"; @Component({
selector: 'app-course-cards',
templateUrl: './course-cards.component.html',
styleUrls: ['./course-cards.component.css']
})
export class CourseCardsComponent { constructor(private router: Router, private route: ActivatedRoute) {
} navigate(path) {
this.router.navigate([{outlets: {primary: path, sidemenu:path}}], {relativeTo: this.route});
}
}
    <div class="col-xs-6 col-lg-4">
<h2>Development</h2>
<p>Donec id elit non mi porta gravida at eget metus. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Etiam porta sem malesuada magna mollis euismod. Donec sed odio dui. </p>
<p><a class="btn btn-secondary" (click)="navigate('development')" role="button">View details »</a></p>
</div>

So we are using 'navigate' method or Router, set primary and auxiliary router's path to 'development'.

How to get the path information in sidemenu and category detail?

  id: string;
constructor(route: ActivatedRoute, private router: Router) {
route.params.subscribe(
(params) => this.id = params['id']
)
}

Until now, when we click the "View details" button, we can finish the logic, then let's see how it is done from sidemenu.

<div class="col-xs-6 col-sm-3 sidebar-offcanvas" id="sidebar">
<h2>{{id}}</h2>
<div class="list-group">
<a (click)="nav($event, 'development')" class="list-group-item">Development</a>
<a href="#" (click)="nav($event, 'it-software')" class="list-group-item">IT &
Software</a>
<a href="#" (click)="nav($event, 'office')" class="list-group-item">Office
Productivity</a>
<a href="#" (click)="nav($event, 'photo')" class="list-group-item">Photography</a>
<a href="#" (click)="nav($event, 'health')" class="list-group-item">Health and
Fitness</a>
<a href="#" (click)="nav($event, 'music')" class="list-group-item">Music</a>
</div>
</div>
  nav(e, path){
e.preventDefault();
this.router.navigateByUrl(`/courses/(${path}//sidemenu:${path})`);
}

Pretty simple and similar, just using navigateByUrl method, cause here we want to use absoulte router.

[Angular2 Router] Auxiliary Routes bit by bit的更多相关文章

  1. [Angular2 Router] Resolving route data in Angular 2

    From Article: RESOLVING ROUTE DATA IN ANGULAR 2 Github If you know Anuglar UI router, you must know ...

  2. [Angular2 Router] Lazy Load Angular 2 Modules with the Router

    Angular 2 lazy loading is a core feature of Angular 2. Lazy loading allows your application to start ...

  3. 在addroutes后,$router.options.routes没有更新的问题(手摸手,带你用vue撸后台 读后感)

    参照<着手摸手,带你用vue撸后台>一文,本人做了前端的权限判断 https://segmentfault.com/a/1190000009275424 首先就是在addroutes后,$ ...

  4. [Angular2 Router] Configure Auxiliary Routes in the Angular 2 Router - What is the Difference Towards a Primary Route?

    In this tutorial we are going to learn how we can can configure redirects in the angular 2 router co ...

  5. [Angular2 Router] Use Params from Angular 2 Routes Inside of Components

    Angular 2’s ActivatedRoute allows you to get the details of the current route into your components. ...

  6. Angular2 Router路由相关

    路由设置 Angular中路由的配置应该按照先具体路由到通用路由的设置,因为Angular使用先匹配者优先的原则. 示例: 路由设置如下: export const reportRoute: Rout ...

  7. [Angular2 Router] CanActivate Route Guard - An Example of An Asynchronous Route Guard

    In this tutorial we are going to learn how we can to configure an can activate route guard in the An ...

  8. [Angular2 Router] CanDeactivate Route Guard - How To Confirm If The User Wants To Exit A Route

    In this tutorial we are going to learn how we can to configure an exit guard in the Angular 2 Router ...

  9. [Angular2 Router] Optional Route Query Parameters - The queryParams Directive and the Query Parameters Observable

    In this tutorial we are going to learn how to use the Angular 2 router to pass optional query parame ...

随机推荐

  1. POJ 2039 Floyd

    句意理解题 解释输入好啦: 第一行n个数 m场电影 随后m行 每行的第一个数 代表 有k奶牛在这个电影中出现过 随后k个数 是奶牛的编号 如果两头奶牛在同一个电影中出现过 相互度为1 奶牛们的相互度可 ...

  2. Elasticsearch之REST

    REST 简介-定义 REST (REpresentation State Transfer)描述了一个架构样式的网络系统,比如 web 应用程序.它首次出现在 2000 年 Roy Fielding ...

  3. Linux LiveCD 诞生记

    Linux LiveCD 诞生记 650) this.width=650;" onclick='window.open("http://blog.51cto.com/viewpic ...

  4. javafx drag

    public class EffectTest extends Application { @Override public void start(Stage stage) { stage.setTi ...

  5. JS面向对象:

    面向对象:--JS系统对象也是基于原型的程序--不要修改或者添加系统对象下面的方法和属性eg: var arr = [1,2,3]; Array.prototype.push = function() ...

  6. 【DRF视图】

    目录 开始使用内置视图 请结合[DRF序列化]此文献中的数据文件及序列化文件来阅读如下代码. DRF视图为我们提供了非常简便的方法--内置了增删改查等一系列的操作. 我们只需在url中指定对应的方法, ...

  7. 查看JSP和Servlet版本+

    如何查看JSP和Servlet版本 找到jsp-api.jar和servlet-api.jar ,分别打开META-INF下的MAINMEFT.MF文件,查看对应的版本. 例: JSP版本: Mani ...

  8. pdnsd 解析原理

    apt install dnsmasq dnsmasq-fullvim /etc/dnsmasq.conf vim /etc/pdnsd.conf killall pdnsdpdnsd -c /etc ...

  9. 最小生成树(MST,minimum spanning tree)

    生成树:由图生成的树,由图转化为树,进一步可用对树的相关操作来对图进行操作.最小指的是权值最小: 生成树是边的集合,如下图所示的最小生成树:MST={{a,b},{a,f},{f,c}} 本文主要探讨 ...

  10. @JSONField 注解说明

    转自:https://blog.csdn.net/suyimin2010/article/details/80617538 导入@JSONField 注解: import com.alibaba.fa ...