Angular4 组件通讯方法大全
组件通讯,意在不同的指令和组件之间共享信息。如何在两个多个组件之间共享信息呢。
最近在项目上,组件跟组件之间可能是父子关系,兄弟关系,爷孙关系都有。。。。。我也找找了很多关于组件之间通讯的方法,不同的方法应用在不同的场景,根据功能需求选择组件之间最适合的通讯方式。下面我就总结一下关于组件通讯的N多种方法。
1.父→子 input
parent.ts
import { Component } from '@angular/core';@Component({selector: 'page-parent',templateUrl: 'parent.html', }) export class ParentPage {i: number = 0;constructor() {setInterval(() => {this.i++;}, 1000)} }
parent.html<ion-header><ion-navbar><ion-title>Parent</ion-title></ion-navbar> </ion-header><ion-content padding><h2>Parent</h2><page-child [content]="i"></page-child> </ion-content>
child.ts
import { Component,Input } from '@angular/core';@Component({selector: 'page-child',templateUrl: 'child.html', }) export class ChildPage { @Input() content:string;constructor() {} }
child.html <ion-content padding> child:{{content}} </ion-content>
结果:
2.子→父 output
parent.ts
import { Component } from '@angular/core';@Component({selector: 'page-parent',templateUrl: 'parent.html', }) export class ParentPage {i: number = 0;numberIChange(i:number){this.i = i;} }
parent.html
<ion-header><ion-navbar><ion-title>Parent</ion-title></ion-navbar> </ion-header><ion-content padding><h2>Parent:{{i}}</h2><page-child (changeNumber)="numberIChange($event)"></page-child> </ion-content>
child.ts
import { Component, EventEmitter, Output } from '@angular/core';@Component({selector: 'page-child',templateUrl: 'child.html', }) export class ChildPage {@Output() changeNumber: EventEmitter<number> = new EventEmitter();Number: number = 0;constructor() {setInterval(() => {this.changeNumber.emit(++this.Number);}, 1000)} }
child.html <ion-content padding>child </ion-content>
结果:
3.子获得父实例
parent.ts
import { Component } from '@angular/core';@Component({selector: 'page-parent',templateUrl: 'parent.html', }) export class ParentPage {i:number = 0; }
parent.html <ion-header><ion-navbar><ion-title>Parent</ion-title></ion-navbar> </ion-header><ion-content padding><h1>parent: {{i}}</h1><page-child></page-child> </ion-content>
child.ts
import { Component, Input, EventEmitter, Output,Host,Inject,forwardRef } from '@angular/core'; import{ParentPage} from '../parent/parent'; @Component({selector: 'page-child',templateUrl: 'child.html', }) export class ChildPage {constructor( @Host() @Inject(forwardRef(() => ParentPage)) app: ParentPage) {setInterval(() => {app.i++;}, 1000);} }
child.html <ion-content padding>child </ion-content>
结果:
4.父获得子实例
parent.ts
import {ViewChild, Component } from '@angular/core'; import{ChildPage}from '../child/child';@Component({selector: 'page-parent',templateUrl: 'parent.html', }) export class ParentPage { @ViewChild(ChildPage) child:ChildPage;ngAfterViewInit() {setInterval(()=> {this.child.i++;}, 1000)} }
parent.html <ion-header><ion-navbar><ion-title>Parent</ion-title></ion-navbar> </ion-header><ion-content padding><h1>parent {{i}}</h1><page-child></page-child> </ion-content>
child.ts
import { Component, Input, EventEmitter, Output,Host,Inject,forwardRef } from '@angular/core'; @Component({selector: 'page-child',templateUrl: 'child.html', }) export class ChildPage { i:number = 0; }
child.html <ion-content padding> <h2>child {{i}}</h2> </ion-content>
结果:
5.service
parent.ts
import { Component } from '@angular/core'; import{myService}from '../child/myService'@Component({selector: 'page-parent',templateUrl: 'parent.html', }) export class ParentPage {
constructor(service:myService) {setInterval(()=> {service.i++;}, 1000)} }
parent.html <ion-header><ion-navbar><ion-title>Parent</ion-title></ion-navbar> </ion-header><ion-content padding><h1>parent {{i}}</h1><page-child></page-child> </ion-content>
child.ts
import { Component} from '@angular/core'; import{myService}from "../child/myService" @Component({selector: 'page-child',templateUrl: 'child.html', }) export class ChildPage { constructor(public service:myService){} }
child.html <ion-content padding> <h2>child {{service.i}}</h2> </ion-content>
myService.tsps:记得在app.module.ts 加上providers: [KmyService]
import{Injectable } from '@angular/core'; @Injectable() export class KmyService {i:number = 0; }
结果:
6.EventEmitter
myService.ts
import {Component,Injectable,EventEmitter} from '@angular/core'; @Injectable() export class myService {change: EventEmitter<number>;constructor(){this.change = new EventEmitter();} }
parent.ts
import { Component } from '@angular/core'; import{myService}from '../child/myService'@Component({selector: 'page-parent',templateUrl: 'parent.html', }) export class ParentPage {i:number = 0;constructor(service:myService) {setInterval(()=> {service.change.emit(++this.i);}, 1000)} }
parent.html <ion-header><ion-navbar><ion-title>Parent</ion-title></ion-navbar> </ion-header><ion-content padding><h1>parent {{i}}</h1><page-child></page-child> </ion-content>
child.ts
import { Component, EventEmitter} from '@angular/core';import{myService}from "../child/myService" @Component({selector: 'page-child',templateUrl: 'child.html', }) export class ChildPage {i:number = 0;constructor(public service:myService){service.change.subscribe((value:number)=>{this.i = value;})}}
child.html <ion-content padding><h2>child {{i}}</h2> </ion-content>
结果:
7.订阅
parent.ts
import { Component } from '@angular/core'; import{myService}from '../child/myService'@Component({selector: 'page-parent',templateUrl: 'parent.html', }) export class ParentPage {i:number=0;constructor(public service:myService) {setInterval(()=> {this.service.StatusMission(this.i++);}, 1000)} }
parent.html <ion-header><ion-navbar><ion-title>Parent</ion-title></ion-navbar> </ion-header><ion-content padding><h1>parent</h1><page-child></page-child> </ion-content>
child.ts
import { Component, Injectable } from '@angular/core' import { myService } from "../child/myService" import { Subscription } from 'rxjs/Subscription'; @Component({selector: 'page-child',templateUrl: 'child.html', }) export class ChildPage {i:number=0;subscription: Subscription;constructor(private Service: myService) {this.subscription = Service.Status$.subscribe(message => {this.i=message;});}ngOnDestroy() {this.subscription.unsubscribe();} }
child.html <ion-content padding><h2>child {{i}}</h2> </ion-content>
myService.ts
import { Injectable } from '@angular/core'; import { Subject } from 'rxjs/Subject';@Injectable() export class myService {private Source=new Subject<any>();Status$=this.Source.asObservable();StatusMission(message: any) {this.Source.next(message);} }
结果:
以上七种组件与组件的通讯方式,可以选择应用于适合的场景里面,根据情况吧。
此随笔乃本人原创,如有疑问欢迎在下面评论,转载请标明出处。
如果对您有帮助请动动鼠标右下方给我来个赞,您的支持是我最大的动力。
转载于:https://www.cnblogs.com/huangenai/p/7246651.html
Angular4 组件通讯方法大全相关推荐
- Vue3 10多种组件通讯方法
本文简介 点赞 + 关注 + 收藏 = 学会了 本文讲解 Vue 3.2 组件多种通讯方式的基础用法,并且使用了 单文件组件 <script setup> . 众所周知,Vue.js 中一 ...
- Vue3/ Vue3 组件通讯 -- 子传父 方法流程 总结
一 .Vue3 组件通讯 -- 子传父 方法流程 1. 首先子组件通过 defineEmit 写入传递事件名 并 定义变量名 const 变量名one = defineEmit([' 传递事件名']) ...
- Omi教程-组件通讯攻略大全
组件通讯 Omi框架组建间的通讯非常遍历灵活,因为有许多可选方案进行通讯: 通过在组件上声明 data-* 传递给子节点 通过在组件上声明 data 传递给子节点 (支持复杂数据类型的映射) 父容器设 ...
- vue组件通讯六种方法。
props $emit v-model 实现一层子级父级传递. $children $parent 返回的是一个组件集合,但是需要手动添加下标,不推荐使用(组件一但很多,不利于维护). eventBu ...
- IT 巡检内容、方法大全
IT 巡检内容.方法大全 目 录 1. 概述 2. 巡检维度 3. 巡检内容 4. 巡检方法 5. 常用命令.常见问题和解决方法 6. 附录 1 词汇表 7. 附录 2 参考资料 1. ...
- 【VUE3】保姆级基础讲解(二)计算属性,vue组件,vue-cli脚手架,组件通讯,插槽slot
目录 计算属性computed 侦听器watch 对象监听 组件 注册全局组件 注册局部组件 Vue-CLI脚手架 安装和使用 .browserslistrc main.js jsconfig.jso ...
- 【Vue】Vue中的父子组件通讯以及使用sync同步父子组件数据
前言: 之前写过一篇文章<在不同场景下Vue组件间的数据交流>,但现在来看,其中关于"父子组件通信"的介绍仍有诸多缺漏或者不当之处, 正好这几天学习了关于用sync修饰 ...
- Omi框架学习之旅 - 通过对象实例来实现组件通讯 及原理说明
组件通讯不是讲完了吗(上帝模式还没讲哈),怎么又多了种方式啊. 你484傻,多一种选择不好吗? 其实这个不属于组件通讯啦,只是当父组件实例安装和渲染完毕后,可以执行installed这个方法(默认是空 ...
- 一起学React--组件定义和组件通讯
React主打函数式编程,配合上JSX语法,基本上可以把每个模块都封装为单独组件,用组件一时爽,一直用一直爽. 1.函数式组件 在React中最简单的即是创建一个函数式,没有生命周期的组件,这与 Vu ...
最新文章
- Linux下getopt函数的使用
- 编程中new[]和delete[]应该如何使用?
- usb协议规范_你想了解的USB知识,都在这里了!
- CloudStack基本概念-Zone,Pod,Cluster,Host
- 横空出世,席卷Csdn [评微软等公司数据结构+算法面试100题]
- 连接oracle10g数据库免安装oracle客户端解决办法 (转载)
- oracle软件静默安装程序,【oracle】静默安装 oracle 11gr2
- nginx 的请求处理、请求的处理流程
- 智能循迹避障小车C语言程序编写思路,基于单片机的智能小车红外避障循迹系统设计与制作...
- 信息学奥赛C++语言: 求正整数2和n之间的完全数
- SandyMandy ,绝世好BABY http://angel.mingox.com
- jupyter调用py文件_解决Jupyter notebook中.py与.ipynb文件的import问题
- IIS启用GZip压缩
- SQL Server insert的触发器
- shell脚本批量创建用户
- 2020年电工(技师)找答案及电工(技师)考试申请表
- 计算机毕业设计jsp社区养老服务管理系统
- 《关键信息基础设施安全保护条例》正式发布
- 【FPGA】四、按键消抖
- Flyway-数据库迁移工具
热门文章
- CSS教程:li和ul标签用法举例
- Java中有哪些无锁技术来解决并发问题?如何使用?
- 从工具到社区,美图秀秀大规模性能优化实践
- 每个人的宿命都是从文本走向二进制,你也不例外 !
- 一篇文章带你详解 HTTP 协议之报文首部及字段详解(中)
- 【Scratch】青少年蓝桥杯_每日一题_4.13_猫捉老鼠
- python读取一个图像_从图像处理python的文件中读取多个图像
- 义教资料均衡验收计算机室解说词,义教均衡迎检现场会导引解说词
- 蓝牙a2dp硬件卸载是什么意思_索尼这项音频黑科技 让蓝牙音质从此不输有线
- 工信部等六部门公布2021年度国家绿色数据中心名单