一、辅助路由语法

同时控制多个插座内容。

第一步:

模版上除了主插座,还需要声明一个带name属性的插座

第二步:

路由配置中配置name为aux的插座上可以显示哪些组件,比如显示xxx和yyy组件。

第三步:

在导航时候,路由到某个地址时,辅助的插座上显示哪个组件

二、实例

聊天功能可以在任何页面(商品列表页面,商品详情页面,主页面等)使用。

第一步:在app组件的模版上在定义一个插座来显示聊天面板。

<a [routerLink]="['/home']">主页</a>
<a [routerLink]="['/product',2]">商品详情</a>
<input type="button" value="商品详情" (click)="toProductDetails()">
<router-outlet></router-outlet>
<router-outlet name="aux"></router-outlet>

第二步:单独开发一个聊天室组件,只显示在新定义的插座上。

新建聊天组件并修改模版

ng g c chat
<textarea
placeholder="请输入聊天内容" n
class="chat"
ame="" id="" cols="30" rows="10"></textarea>
.chat{
background: green;
height: 100px;
width: 30%;
float: left;
box-sizing: border-box;
}

修改home组件和product组件模版,包一层div并设置样式

.product{
background: yellow;
height: 100px;
width: 70%;
float: left;
box-sizing: border-box;
} .home{
background: red;
height: 100px;
width: 70%;
float: left;
box-sizing: border-box;
}

配置路由决定聊天组件是否显示

const routes: Routes = [
{ path: '', redirectTo : 'home',pathMatch:'full' },
{ path: 'chat', component: ChatComponent, outlet: "aux"},//辅助路由
{ path: 'home', component: HomeComponent },
{ path: 'product/:id', component: ProductComponent, children:[
{ path: '', component : ProductDescComponent },
{ path: 'seller/:id', component : SellerInfoComponent }
] },
{ path: '**', component: Code404Component }
];

第三步:通过路由参数控制新插座是否显示聊天面板

增加开始聊天和结束聊天按钮

<!--The content below is only a placeholder and can be replaced.-->
<a [routerLink]="['/home']">主页</a>
<a [routerLink]="['/product',2]">商品详情</a>
<input type="button" value="商品详情" (click)="toProductDetails()"> <a [routerLink]="[{outlets: {aux: 'chat'}}]">开始聊天</a>
<a [routerLink]="[{outlets: {aux: null}}]">结束聊天</a> <router-outlet></router-outlet>
<router-outlet name="aux"></router-outlet>

效果:

点开始聊天,URL后面多了一个(aux:chat) :辅助路由aux插座路由路径是chat,chat路径对应显示ChatComponent。

主路由可随意切换,不影响辅助路由

http://localhost:4200/home(aux:chat)
http://localhost:4200/product/2(aux:chat)

扩展:如果想展示chat组件时候,主路由要跳到home组件上。

用priamry指定主路由路径,因为主路由没有名字。

<a [routerLink]="[{outlets: {primary: 'home' , aux: 'chat'}}]">开始聊天</a>

本文作者starof,因知识本身在变化,作者也在不断学习成长,文章内容也不定时更新,为避免误导读者,方便追根溯源,请诸位转载注明出处:http://www.cnblogs.com/starof/p/9006227.html 有问题欢迎与我讨论,共同进步。

Angular路由——辅助路由的更多相关文章

  1. Angular 4 辅助路由

    1.辅助路由 2. 创建chat组件 ng g component chat 3. 组件html css: .chat{ background:green; height:100px; width:2 ...

  2. angular路由详解五(辅助路由)

    在HTML文件中 //主路由 <router-outlet></router-outlet> //辅助路由 <router-outlet  name="aux& ...

  3. Angular路由——子路由

    一.子路由语法: 二.实例 在商品详情页面,除了显示商品id信息,还显示了商品描述,和销售员的信息. 通过子路由实现商品描述组件和销售员信息组件展示在商品详情组件内部. 1.新建2个组件修改其内容 n ...

  4. Angular : 绑定, 参数传递, 路由

    如何把jquery导入angular npm install jquery --savenpm install @type/jquery --save-dev "node_modules/z ...

  5. angular.js的路由和模板在asp.net mvc 中的使用

    angular.js的路由和模板在asp.net mvc 中的使用 我们知道angular.js是基于mvc 的一款优秀js框架,它也有一套自己的路由机制,和asp.net mvc 路由不太一样.as ...

  6. angular4 辅助路由

    1.辅助路由 2. 创建chat组件 ng g component chat 3. 组件html css: 1 2 3 4 5 6 7 .chat{   background:green;   hei ...

  7. Angular routing生成路由和路由的跳转

    Angular routing生成路由和路由的跳转 什么是路由 路由的目的是可以让根组件按照不同的需求动态加载不同的组件. 根据不同地址,加载不同组件,实现单页面应用. Angular 命令创建一个配 ...

  8. Angular学习笔记—路由(转载)

    创建路由 1.首先安装 Angular Router.你可以通过运行以下任一操作来执行此操作: yarn add @angular/router # OR npm i --save @angular/ ...

  9. Angular 监听路由变化

    var app = angular.module('Mywind',['ui.router']) //Angular 监听路由变化 function run($ionicPlatform, $loca ...

随机推荐

  1. Python——Tk控件说明

    控件 描述 具体说明 Button 按钮  点此链接 Canvas 提供绘图形状的功能,包含图像和位图  点此链接 Checkbutton 多选框  点此链接 Entry 单行文本框  点此链接 Fr ...

  2. Mybatis Generator的model生成中文注释,支持oracle和mysql(通过实现CommentGenerator接口的方法来实现)

    自己手动实现的前提,对maven项目有基本的了解,在本地成功搭建了maven环境,可以参考我之前的文章:maven环境搭建 项目里新建表时model,mapper以及mapper.xml基本都是用My ...

  3. 第四十篇-private,public,protected的区别

    1.public: public表明该数据成员.成员函数是对所有用户开放的,所有用户都可以直接进行调用 2.private: private表示私有,私有的意思就是除了class自己之外,任何人都不可 ...

  4. LVM备份(3)- pg_dumpall

  5. Vue(小案例_vue+axios仿手机app)_购物车

    一.前言 1.购物车 二.主要内容 1.效果演示如下,当我们选择商品数量改变的时候,也要让购物车里面的数据改变 2.具体实现 (1)小球从上面跳到下面的效果 (2)当点击上面的“加入购物车按钮”让小球 ...

  6. js获取url参数(通用方法)

    function getUrl(name="") { var url = location.search; //获取url中"?"符后的字串 var theRe ...

  7. 1.5分布式通讯框架-RMI

    分布式通信框架-RMI讲解 什么是RPC Remote procedure call protocal RPC协议其实是一个规范.常用PRC框架:Dubbo.Thrif.RMI.Webservice. ...

  8. 第十一节:基于MVC5+Spring.Net+EF+Log4net 传统的一种搭建模式

  9. MyBatis使用注意事项

    目录 1. 使用何种映射器配置 2. 对象生命周期和作用域 SqlSessionFactoryBuilder SqlSessionFactory SqlSession 映射器实例(Mapper Ins ...

  10. Contest1874 - noip基础知识五:动态规划(背包、树dp、记忆化、递推、区间、序列dp、dp优化)

    传送门 T1  dp[n][m]=dp[n-1][m-1]+dp[n-m][m] T2  ans=cat(n)*(n!)2  卡特兰数 T3  dp[i][j]=sigma(dp[i-1][j-a[i ...