angular组件之间的通讯
组件通讯,意在不同的指令和组件之间共享信息。如何在两个多个组件之间共享信息呢。
最近在项目上,组件跟组件之间可能是父子关系,兄弟关系,爷孙关系都有。。。。。我也找找了很多关于组件之间通讯的方法,不同的方法应用在不同的场景,根据功能需求选择组件之间最适合的通讯方式。下面我就总结一下关于组件通讯的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.父获得子实例 (
把子组件作为 ViewChild,注入到父组件里面
)
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.ts
ps:记得在app.module.ts 加上providers: [KmyService]
import{Injectable } from '@angular/core';
@Injectable()
export class KmyService {
i:number = 0;
}
结果:
6.EventEmitter(父组件emit 子组件订阅)
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);
}
}
结果:
angular组件之间的通讯的更多相关文章
- Angular组件之间通讯
组件之间会有下列3种关系: 1. 父子关系 2. 兄弟关系 3. 没有直接关系 通常采用下列方式处理(某些方式是框架特有)组件间的通讯,如下: 1父子组件之间的交互(@Input/@Output/模板 ...
- Angular10 组件之间的通讯
1 父组件和子组件之间的通讯 2 利用中间组件实现两个组件之间的通讯 3 利用服务实现两个组件之间的通讯 2017年8月26日20:09:13 待更新... 1 组件之间的关系图 1.1 父子关系 1 ...
- 组件之间的通讯:vuex状态管理,state,getters,mutations,actons的简单使用(一)
之前的文章中讲过,组件之间的通讯我们可以用$children.$parent.$refs.props.data... 但问题来了,假如项目特别大,组件之间的通讯可能会变得十分复杂... 这个时候了我们 ...
- vue.js组件之间的通讯-----父亲向儿子传递数据,儿子接收父亲的数据
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Vue父子组件之间的通讯(学习笔记)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- angular组件之间的通信
一.组件创建 直接使用 ng g component 的命令创建组件,会自动生成组件所需的文件,方便快捷. // 父组件 ng g component parent // 子组件 ng g compo ...
- angular 组件之间传值
/** * Created by Administrator on 2017/8/28. */ var app =angular.module('app',[]); app.directive('fo ...
- Angular 组件之间的传值
第一种方法(传单个或者多个参数): 主页面方法: 先添加引用:private _routes: Router, Details(PBSCode) { this._routes.navigate(['p ...
- Angular 发布订阅模式实现不同组件之间通讯
在我们项目中要实现不同组件之间通讯,Angular的@Input和@Output只能实现有父子组件的限制,如果是复杂跨组件实现不同组件可以通过共享变量的方式实现,比如这个博客的思路:https://w ...
随机推荐
- centos7安装tomcat8 新手入门 图文教程
系统环境 操作系统:64位CentOS Linux release 7.2.1511 (Core) JDK版本:1.8.0_121 下载tomcat8压缩包 访问官网:http://tomcat.ap ...
- mysql 事件 按月分表
/****** 对象: Table Order_201512 脚本日期: 2015/12/18 11:44:23 ******/ /****** 字段数据长度 = 2599 字节 ******/ CR ...
- Ubuntu16 源码方式安装postgresql数据库
依赖工具库 注意:默认用户名是postgres,以下命令是Ubuntu操作系统中的命令 make GCC Zlib 安装命令:sudo apt-get install zlib1g-dev注意有些软件 ...
- 《剑指offer》第六十五题(不用加减乘除做加法)
// 面试题65:不用加减乘除做加法 // 题目:写一个函数,求两个整数之和,要求在函数体内不得使用+.-.×.÷ // 四则运算符号. #include <iostream> int A ...
- unittest单元测试简单介绍
unittest单元测试框架不仅可以适用于单元测试,还可以适用WEB自动化测试用例的开发与执行,该测试框架可组织执行测试用例,并且提供了丰富的断言方法,判断测试用例是否通过,最终生成测试结果.今天笔者 ...
- (转)Nandflash读写
------------------------------------------------------------------------------------------文章1------- ...
- PhantomJS框架(初识无头浏览器)
博主今天看到大神聊起 headless,首先我去了解了下这个概念 无头浏览器 selenium框架是有头浏览器的代表,即可看得见的浏览器 而headless browser无头浏览器,即看不见的浏览 ...
- 20165327《Java程序设计》实验一 Java开发环境的熟悉 实验报告
20165327<Java程序设计>实验二 <Java面向对象程序设计>实验报告 实验二 <Java面向对象程序设计> 一.实验报告封面 课程:Java程序设计 班 ...
- pip3 install requests Cannot open D:\Python35\Scripts\pip3-script.py
1.问题描述: 使用pip(或pip3)指令安装模块时,出现了Cannot open D:\Python35\Scripts\pip3-script.py的报错信息 2.原因分析: pip安装出错 3 ...
- LeetCode--682--棒球比赛(java)
你现在是棒球比赛记录员. 给定一个字符串列表,每个字符串可以是以下四种类型之一:1.整数(一轮的得分):直接表示您在本轮中获得的积分数.2. "+"(一轮的得分):表示本轮获得的得 ...