Angular中不同的组件间传值与通信的方法
主要分为父子组件和非父子组件部分。
父子组件间参数与通讯方法
使用事件通信(EventEmitter,@Output):
场景:可以在父子组件之间进行通信,一般使用在子组件传递消息给父组件;
步骤:
- 子组件创建事件EventEmitter对象,使用@output公开出去;
- 父组件监听子组件@output出来的方法,然后处理事件。
代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
// child 组件 @Component({ selector: 'app-child' , template: '' , styles: [``] }) export class AppChildComponent implements OnInit { @Output() onVoted: EventEmitter<any> = new EventEmitter(); ngOnInit(): void { this .onVoted.emit(1); } } // parent 组件 @Component({ selector: 'app-parent' , template: ` <app-child (onVoted)= "onListen($event)" ></app-child> `, styles: [``] }) export class AppParentComponent implements OnInit { ngOnInit(): void { throw new Error( 'Method not implemented.' ); } onListen(data: any): void { console.log( 'TAG' + '---------->>>' + data); } } |
使用@ViewChild和@ViewChildren:
场景:一般用于父组件给子组件传递信息,或者父组件调用子组件的方法;
步骤:
- 父组件里面使用子组件;
- 父组件里面使用@ViewChild获得子组件对象。
- 父组件使用子组件对象操控子组件;(传递信息或者调用方法)。
代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
// 子组件 @Component({ selector: 'app-child' , template: '' , styles: [``] }) export class AppChildComponent2 implements OnInit { data = 1; ngOnInit(): void { } getData(): void { console.log( 'TAG' + '---------->>>' + 111); } } // 父组件 @Component({ selector: 'app-parent2' , template: ` <app-child></app-child> `, styles: [``] }) export class AppParentComponent2 implements OnInit { @ViewChild(AppChildComponent2) child: AppChildComponent2; ngOnInit(): void { this .child.getData(); // 父组件获得子组件方法 console.log( 'TAG' + '---------->>>' + this .child.data); // 父组件获得子组件属性 } } |
非父子组件参数传递与通讯方法
通过路由参数
场景:一个组件可以通过路由的方式跳转到另一个组件 如:列表与编辑
步骤:
- A组件通过routerLink或router.navigate或router.navigateByUrl进行页面跳转到B组件
- B组件接受这些参数
此方法只适用于参数传递,组件间的参数一旦接收就不会变化
代码
传递方式
routerLink
1
2
3
4
5
|
<a routerLink=[ "/exampledetail" ,id]></a> routerLink=[ "/exampledetail" ,{queryParams:object}] routerLink=[ "/exampledetail" ,{queryParams: 'id' : '1' , 'name' : 'yxman' }]; |
router.navigate
1
2
|
this .router.navigate([ '/exampledetail' ,id]); this .router.navigate([ '/exampledetail' ],{queryParams:{ 'name' : 'yxman' }}); |
router.navigateByUrl
1
2
|
this .router.navigateByUrl( '/exampledetail/id' ); this .router.navigateByUrl( '/exampledetail' ,{queryParams:{ 'name' : 'yxman' }}); |
传参方传参之后,接收方2种接收方式如下:
snapshot
1
2
3
4
5
6
7
8
|
import { ActivateRoute } from '@angular/router' ; public data: any; export class ExampledetailComponent implements OnInit { constructor( public route: ActivateRoute ) { }; ngOnInit(){ this .data = this .route.snapshot.params[ 'id' ]; }; } |
queryParams
1
2
3
4
5
6
7
8
9
|
import { ActivateRoute } from '@angular/router' ; export class ExampledetailComponent implements OnInit { public data: any; constructor( public activeRoute:ActivateRoute ) { }; ngOnInit(){ this .activeRoute.queryParams.subscribe(params => { this .data = params[ 'name' ]; }); }; |
使用服务Service进行通信,即:两个组件同时注入某个服务
场景:需要通信的两个组件不是父子组件或者不是相邻组件;当然,也可以是任意组件。
步骤:
- 新建一个服务,组件A和组件B同时注入该服务;
- 组件A从服务获得数据,或者想服务传输数据
- 组件B从服务获得数据,或者想服务传输数据。
代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
// 组件A @Component({ selector: 'app-a' , template: '' , styles: [``] }) export class AppComponentA implements OnInit { constructor(private message: MessageService) { } ngOnInit(): void { // 组件A发送消息3 this .message.sendMessage(3); const b = this .message.getMessage(); // 组件A接收消息; } } // 组件B @Component({ selector: 'app-b' , template: ` <app-a></app-a> `, styles: [``] }) export class AppComponentB implements OnInit { constructor(private message: MessageService) { } ngOnInit(): void { // 组件B获得消息 const a = this .message.getMessage(); this .message.sendMessage(5); // 组件B发送消息 } } |
消息服务模块
场景:这里涉及到一个项目,里面需要实现的是所有组件之间都有可能通信,或者是一个组件需要给几个组件通信,且不可通过路由进行传参。
设计方式:
- 使用RxJs,定义一个服务模块MessageService,所有的信息都注册该服务;
- 需要发消息的地方,调用该服务的方法;
- 需要接受信息的地方使用,调用接受信息的方法,获得一个Subscription对象,然后监听信息;
- 当然,在每一个组件Destory的时候,需要
1
|
this .subscription.unsubscribe(); |
代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
// 消息中专服务 @Injectable() export class MessageService { private subject = new Subject<any>(); /** * content模块里面进行信息传输,类似广播 * @param type 发送的信息类型 * 1-你的信息 * 2-你的信息 * 3-你的信息 * 4-你的信息 * 5-你的信息 */ sendMessage(type: number) { console.log( 'TAG' + '---------->>>' + type); this .subject.next({type: type}); } /** * 清理消息 */ clearMessage() { this .subject.next(); } /** * 获得消息 * @returns {Observable<any>} 返回消息监听 */ getMessage(): Observable<any> { return this .subject.asObservable(); } } // 使用该服务的地方,需要注册MessageService服务; constructor(private message: MessageService) { } // 消息接受的地方; public subscription: Subscription; ngAfterViewInit(): void { this .subscription = this .message.getMessage().subscribe(msg => { // 根据msg,来处理你的业务逻辑。 }) } // 组件生命周期结束的时候,记得注销一下,不然会卡; ngOnDestroy(): void { this .subscription.unsubscribe(); } // 调用该服务的方法,发送信息; send():void { this .message.sendMessage(‘我发消息了,你们接受下'); // 发送信息消息 } |
这里的MessageService,就相当于使用广播机制,在所有的组件之间传递信息;不管是数字,字符串,还是对象都是可以传递的,而且这里的传播速度也是很快的。
Angular中不同的组件间传值与通信的方法的更多相关文章
- Vue 组件间传值
前言 Vue 作为现在比较火的框架之一,相信您在使用的过程中,也会遇到组件间传值的情况,本文将讲解几种 Vue 组件间传值的几种方法,跟着小编一起来学习一下吧! 实现 注意: 学习本文,需要您对 Vu ...
- Vue中组件间传值常用的几种方式
版本说明: vue-cli:3.0 一.父子组件间传值 1.props/$emit -父组件==>>子组件: 子组件中通过定义props接收父组件中通过v-bind绑定的数据 父组件代码 ...
- Vue学习(二)-Vue中组件间传值常用的几种方式
版本说明:vue-cli:3.0 主要分为两类: 1.父子组件间的传值 2.非父子组件间的传值 1.父子组件间传值 父组件向子组件传值 第一种方式: props 父组件嵌套的子组件中,使用v-bind ...
- react组件间传值详解
一.父子组件间传值 <1>父传子 父组件:
- Angular : 响应式编程, 组件间通信, 表单
Angular 响应式编程相关 ------------------------------------------------------------------------------------ ...
- React Native 系列(五) -- 组件间传值
前言 本系列是基于React Native版本号0.44.3写的.任何一款 App 都有界面之间数据传递的这个步骤的,那么在RN中,组件间是怎么传值的呢?这篇文章将介绍到顺传.逆传已经通过通知传值. ...
- Vue组件间传值 v-model
使用过Vue的同学应该都了解组件之间传值 父组件 --> 子组件 : props 子组件 --> 父组件 : 事件 其实有一种更为简单的方法,是基于上述两种方法,那就是 v-model 我 ...
- vue——父子组件间传值
(1)父组件给子组件传值(商品详情页): 根据订单类型,判断显示立即购买/立即拼单: 通过props来传递参数 父组件(商品详情页) 父组件调用子组件,在子组件的标签中,通过:数据名称=”数据”的形式 ...
- React 组件间传值
壹 .了解React传值的数据 一. 创建组件的方法 一 . 1 通过function声明的组件特点是: 1)function创建的组件是没有state属性,而state属性决定它是不是有生命周期 ...
随机推荐
- Spring boot 自定义拦截器
1.新建一个类实现HandlerInterceptor接口,重写接口的方法 package com.zpark.interceptor; import com.zpark.tools.Constant ...
- Storm实现实时大数据分析(storm介绍,与Hadoop比较,)
一.storm与Hadoop对比 Hadoop: 全量数据处理使用的大多是鼎鼎大名的hadoop或者hive,作为一个批处理系统,hadoop以其吞吐量大.自动容错等优点,在海量数据处理上得到了广泛的 ...
- [UNITY 5.4 UGUI] 模态对话框
1.建立两个画布 a.背景界面 b.置顶界面(添加一个 panel 控件) 2.修改置顶界面中 panel ,添加属性 [Canvas Group] 3.根据界面设计情况修改透明度,色彩,图片
- oracle SQL多表查询
SQL多表查询 1.集合理论 1.1 什么是集合 具有某种特定性质的事物的总体. 集合的特性:无序性.互异性.确定性. 一个集合可以小到从一个表中取出一行中的一列. 1 ro ...
- 12Linux_Apache_vsftpd(匿名开发模式)
网站:让我们的用户可以通过浏览器去访问到的文档的资源. windows:IIS Linux:Apache Nginx(吃得少,干的多) APACHE:基金会,公司,软件 httpd:软件名称,软件包名 ...
- java高并发实战(一)——为什么需要并发
转自:https://blog.csdn.net/gududedabai/article/details/80813592
- delphi frame 添加 create onshow 事件
delphi frame 添加 create onshow 事件 https://www.cnblogs.com/Gufan/archive/2011/09/06/2538932.html proc ...
- JeeWx全新版本发布!捷微二代微信活动平台1.0发布!活动插件持续开源更新!
JeeWx捷微二代微信活动平台 (专业微信营销活动平台,活动插件持续更新ing~) 终于等到你!还好我没放弃! 在团队持续多年的努力下,Jeewx微信管家和H5活动平台不断更新迭代,积累了许许多 ...
- Django04-模板系统Template
一.模板支持的语法 Django模板中只需要记两种特殊符号: {{ }}表示变量,在模板渲染的时候替换成值{% %}表示逻辑相关的操作. 二. 变量(使用双大括号来引用变量) 1.语法格式:{{var ...
- 二.第一个自动化demo,打开APP-如何获取包名和activity。(真机)
环境配置成功后,我们就可以进行第一个自动化测试了.用真机则不需要安装安卓模拟器.以一个简单的打开APP为例. 一.获取包名和activtity 启动一个app,我们需要知道它的平台.版本号. ...