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. JavaScript--数据结构算法之链表

    数组的缺点:数组的长度固定,增删时比较困难要移动元素,而且数据填满再添加元素比较复杂.js:数组有split(),可以任意的分割.不存在上述问题.主要问题是:js数组都被实现成了对象,和其他语言的数组 ...

  2. 小米开源文件管理器MiCodeFileExplorer-源码研究(9)-入口分析

    AndroidManifest.xml是Android应用程序最重要的配置文件. 入口文件和intent-filter <application android:icon="@draw ...

  3. STM32上使用JSON

    一.STM32工程中添加JSON 最近在一网2串项目,串口和网口之间可能需要定义一下简单的通信协议,而通信协议上则需要去定义一下通信的数据格式,上次听剑锋说要用Json来定义,目前查了下资料具体如何去 ...

  4. hdoj 2122 Ice_cream’s world III【最小生成树】

    Ice_cream's world III Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  5. shell date 命令说明

    shell date 命令说明 使用方法:date [选项]... [+格式] 或:date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]] 以给定的格式 ...

  6. Centos 6.8 安装 Protocol Buffers , v3.2.0有 BUG ,安装 3.1.0

    Centos 6.8 安装 Protocol Buffers   , v3.2.0有 BUG ,安装 3.1.0 切换到用户目录 cd ~ 安装 python2.7,须加入zlib wget http ...

  7. js18--继承方式

    方式1:子类.prototype = 父类对象 Boy.prototype = new Person(); Sub.prototype = new Sup('张三');   //可以传参数也可以不传 ...

  8. 初步使用RecyclerView实现瀑布流

    先看效果 关于RecyclerView,真的是很强大. 个人觉得主要方便的地方是 1.直接可以设置条目布局,通过setLayoutManager LinearLayoutManager:线性布局,横向 ...

  9. 28.lambda表达式与多线程

    #include <iostream> #include <thread> #include <Windows.h> #include <chrono> ...

  10. Rpm另类用法加固Linux安全

    Rpm另类用法加固Linux安全   RPM是Red Hat Package Manager的缩写即Red Hat软件管理器.它是一个开放的包管理软件,由Red Hat公司所开发和维护,可以在Red ...