1 父组件和子组件之间的通讯

2 利用中间组件实现两个组件之间的通讯

3 利用服务实现两个组件之间的通讯

2017年8月26日20:09:13

  待更新...

1 组件之间的关系图

  1.1 父子关系

  1.2 兄弟关系

  1.3 没啥关系

  

2 组件之间有以下几种典型的通讯方案

  2.1 直接父子关系

    2.1.1 父组件直接访问子组件的 public 属性和方法

      技巧01:父组件的视图中调用子组件的方法需要利用模板变量

      技巧02:父组件的类中调用子组件的方法需利用@ViewChild装饰器

    2.1.2 借助于 @Input 和 @Output 进行通讯

      就是利用输入属性和输出属性来实现

      技巧01:输入属性和输出属性需要用到属性绑定和事件绑定相关的知识

  2.2 兄弟关系

    利用他们共同的父组件进行通信【有点麻烦】

    技巧01:此时他们的父组件相当于一个中间人

  2.3 适用于任何关系的方法

    2.3.1 借助于service单例进行通讯

    2.3.2 利用cookie或者localstorage进行通讯

    2.3.3 利用session进行通讯

3 直接调用

  对于父子组件而言,父组件可以直接调用子组件的public型属性和方法

  缺点:如果父组件直接访问子组件,那么两个组件之间的关系就被固定死了。父子两个组件紧密依赖,谁也离不开谁,也就都不能单独使用了。所以,除非你知道自己在做什么,最好不要直接在父组件里面直接访问子组件上的属性和方法,以免未来一改一大片。

  3.1 利用模板变量实现

    

  3.2 利用@ViewChild实现

    

  3.3 代码汇总

    》父组件

<div class="panel panel-primary">
<div class="panel-heading">父组件</div>
<div class="panel-body">
<child #child1 ></child>
<p>
<label>获取子组件的info属性值为:</label>
<span>{{child1.info}}</span>
</p>
<p>
<button (click)="child1.greeting('王杨帅');">调用子组件的greeting方法</button>
</p>
</div>
<div class="panel-footer">{{currentDate | date : "yyyy-MM-dd HH:mm:ss"}}</div>
</div>

HTML

import { Component, OnInit, ViewChild } from '@angular/core';
import { ChildComponent } from './child/child.component';
import { AfterViewInit, AfterContentChecked, AfterContentInit, AfterViewChecked } from '@angular/core/src/metadata/lifecycle_hooks'; @Component({
selector: 'parent',
templateUrl: './parent.component.html',
styleUrls: ['./parent.component.scss']
})
export class ParentComponent implements OnInit { @ViewChild("child1")
child1 : ChildComponent; currentDate : Date; constructor() { } ngOnInit() {
this.currentDate = new Date();
// setInterval(() => {
// this.currentDate = new Date();
// }, 1000); this.child1.greeting("三少");
} }

TS

    》子组件

<div class="panel panel-primary">
<div class="panel-heading">子组件</div>
<div class="panel-body"> </div>
<div class="panel-footer">{{currentDate | date : "yyyy-MM-dd HH:mm:ss"}}</div>
</div>

HTML

import { Component, OnInit, DoCheck,AfterViewChecked, EventEmitter, AfterContentInit, Output, Input, OnChanges, SimpleChanges, AfterViewInit, AfterContentChecked } from '@angular/core';

@Component({
selector: 'child',
templateUrl: './child.component.html',
styleUrls: ['./child.component.scss']
})
export class ChildComponent implements OnInit { currentDate : Date; info : string; constructor() { } ngOnInit() {
this.info = "子组件中的info属性";
this.currentDate = new Date();
// setInterval(() => {
// this.currentDate = new Date();
// }, 1000);
} greeting(name : string) : void {
alert("子组件中greeting方法的返回值: Helo " + name);
console.log("子组件中greeting方法的返回值: Helo " + name);
} }

TS

4 @Input 和 @Output

  就是通过属性绑定来实现父组件向子组件的输入属性船值;通过事件绑定来实现子组件向父组件传值

  技巧01:其实可以直接利用双向绑定来实现

  4.1 父组件向子组件传值

    子组件利用@Input定义一个输入属性,父组件视图中在子组件的标签上利用属性绑定来实现

  4.2 子组件向父组件传值

    子组件利用@Output定义一个输出属性,父组件视图中在子组件标签上利用事件来实现

  4.3 代码汇总

    》父组件

<div class="panel panel-primary">
<div class="panel-heading">父组件</div>
<div class="panel-body"> <!-- 这三种写法都可以实现 -->
<!-- <child [info]="parentInfo" (infoChange)="onInfoChange($event)" ></child> -->
<!-- <child [info]="parentInfo" (infoChange)="parentInfo=$event" ></child> -->
<child [(info)]="parentInfo" ></child> <p>
{{parentInfo}}
</p>
</div>
<div class="panel-footer">{{currentDate | date : "yyyy-MM-dd HH:mm:ss"}}</div>
</div>

HTML

import { Component, OnInit, ViewChild } from '@angular/core';
import { ChildComponent } from './child/child.component';
import { AfterViewInit, AfterContentChecked, AfterContentInit, AfterViewChecked } from '@angular/core/src/metadata/lifecycle_hooks'; @Component({
selector: 'parent',
templateUrl: './parent.component.html',
styleUrls: ['./parent.component.scss']
})
export class ParentComponent implements OnInit { parentInfo : string; currentDate : Date; constructor() { } ngOnInit() {
this.parentInfo = "王杨帅";
this.currentDate = new Date();
// setInterval(() => {
// this.currentDate = new Date();
// }, 1000);
} onInfoChange(info : string) {
this.parentInfo = info;
} }

TS

    》子组件

<div class="panel panel-primary">
<div class="panel-heading">子组件</div>
<div class="panel-body">
<p>
输入属性info的值为:{{info}}
</p>
<p>
<button (click)="onTest()" >点击向父组件发送数据</button>
</p>
</div>
<div class="panel-footer">{{currentDate | date : "yyyy-MM-dd HH:mm:ss"}}</div>
</div>

HTML

import { Component, OnInit, DoCheck,AfterViewChecked, EventEmitter, AfterContentInit, Output, Input, OnChanges, SimpleChanges, AfterViewInit, AfterContentChecked } from '@angular/core';

@Component({
selector: 'child',
templateUrl: './child.component.html',
styleUrls: ['./child.component.scss']
})
export class ChildComponent implements OnInit { currentDate : Date; @Input()
info : string; @Output()
infoChange : EventEmitter<string> = new EventEmitter(); constructor() { } ngOnInit() {
this.currentDate = new Date();
// setInterval(() => {
// this.currentDate = new Date();
// }, 1000);
} onTest() : void {
this.infoChange.emit("子组件传过来的数据");
} }

TS

5 服务实现

  待更新...

6 cookie或localstorage实现

  6.1 原理图

    

  6.2 代码汇总

    》工具组件

      利用投影来简化代码

<div class="panel panel-primary">
<div class="panel-heading">
<ng-content select=".heading"></ng-content>
</div>
<div class="panel-body">
<ng-content select=".body"></ng-content>
</div>
<div class="panel-footer">
{{currentDate | date : "yyyy-MM-dd HH:mm:ss"}}
</div>
</div>

HTML

import { Component, OnInit } from '@angular/core';
import { setInterval } from 'timers'; @Component({
selector: 'panel',
templateUrl: './panel.component.html',
styleUrls: ['./panel.component.scss']
})
export class PanelComponent implements OnInit { currentDate : Date; constructor() { } ngOnInit() {
this.currentDate = new Date();
setInterval(() => {
this.currentDate = new Date();
}, 1000);
} }

TS

    》测试组件01

<panel>
<div class="heading">
测试组件01
</div>
<div class="body">
<button (click)="onClick()">写入数据到</button>
</div>
</panel>

HTML

import { Component, OnInit } from '@angular/core';
import { TestService } from '../services/test.service'; @Component({
selector: 'test01',
templateUrl: './test01.component.html',
styleUrls: ['./test01.component.scss']
})
export class Test01Component implements OnInit { constructor(
) { } ngOnInit() {
} onClick() : void {
// 将对象转化成JSON字符串并存储道浏览器缓存中
window.localStorage.setItem("user", JSON.stringify({name: "王杨帅", age: 9}));
} }

TS

    》测试组件02

<panel>
<div class="heading">
测试组件02
</div>
<div class="body">
<p>
<button (click)="onClick()">获取数据</button>
</p>
</div>
</panel>

HTML

import { Component, OnInit } from '@angular/core';
import { TestService } from '../services/test.service'; @Component({
selector: 'test02',
templateUrl: './test02.component.html',
styleUrls: ['./test02.component.scss']
})
export class Test02Component implements OnInit { constructor(
) { } ngOnInit() {
} onClick() : void {
// 从浏览器缓存中获取数据【PS: 获取到的是string类型的数据】
let data = localStorage.getItem("user");
console.log(data); // 将JSON字符串转化成对象
let json_data = JSON.parse(data); console.log(json_data.name);
window.localStorage.removeItem("user");
} }

TS

7 session实现

  待更新...

Angular10 组件之间的通讯的更多相关文章

  1. 组件之间的通讯:vuex状态管理,state,getters,mutations,actons的简单使用(一)

    之前的文章中讲过,组件之间的通讯我们可以用$children.$parent.$refs.props.data... 但问题来了,假如项目特别大,组件之间的通讯可能会变得十分复杂... 这个时候了我们 ...

  2. vue.js组件之间的通讯-----父亲向儿子传递数据,儿子接收父亲的数据

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. angular组件之间的通讯

    组件通讯,意在不同的指令和组件之间共享信息.如何在两个多个组件之间共享信息呢. 最近在项目上,组件跟组件之间可能是父子关系,兄弟关系,爷孙关系都有.....我也找找了很多关于组件之间通讯的方法,不同的 ...

  4. Vue父子组件之间的通讯(学习笔记)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. Angular组件之间通讯

    组件之间会有下列3种关系: 1. 父子关系 2. 兄弟关系 3. 没有直接关系 通常采用下列方式处理(某些方式是框架特有)组件间的通讯,如下: 1父子组件之间的交互(@Input/@Output/模板 ...

  6. Angular 发布订阅模式实现不同组件之间通讯

    在我们项目中要实现不同组件之间通讯,Angular的@Input和@Output只能实现有父子组件的限制,如果是复杂跨组件实现不同组件可以通过共享变量的方式实现,比如这个博客的思路:https://w ...

  7. vue组件的基本使用,以及组件之间的基本传值方式

    组件(页面上的每一个部分都是组件) 1.三部分:结构(template),样式(style),逻辑(script) 2.组件的作用:复用 3.模块包含组件 4.组件创建:     1.全局组件:Vue ...

  8. Angular学习笔记之组件之间的交互

    1.@Input:可设置属性 当它通过属性绑定的形式被绑定时,值会“流入”这个属性. 在子组件中使用,例如:@Input()name:string 父组件定义宾亮,并在父组件的模板中绑定,例如: 子组 ...

  9. React组件间的通讯

    组件化开发应该是React核心功能之一,组件之间的通讯也是我们做React开发必要掌握的技能.接下来我们将从组件之间的关系来分解组件间如何传递数据. 1.父组件向子组件传递数据 通讯是单向的,数据必须 ...

随机推荐

  1. shiro2

    mapper接口:根据用户id查询用户权限的菜单 service接口:根据用户id查询用户权限的菜单 获取用户权限范围的url 思路: 在用户认证时,认证通过,根据用户id从数据库获取用户权限范围的u ...

  2. iOS Code Signing: 解惑详解

    iPhone开发的代码签名 代码签名确保代码的真实以及明确识别代码的来源.在代码运行在一个开发系统以前,以及在代码提交到Apple发布以前,Apple要求所有的的应用程序都必须进行数字签名.另外,Ap ...

  3. vim配置与使用

    Vim 是一个上古神器,本篇文章主要持续总结使用 Vim 的过程中不得不了解的一些指令和注意事项,以及持续分享一个前端工作者不得不安装的一些插件,而关于 Vim 的简介,主题的选择,以及为何使用 vi ...

  4. Hadoop 2.x简介

    Hadoop 2.0产生背景 Hadoop1.0中HDFS和MapReduce在高可用.扩展性等方面存在问题 HDFS存在的问题 NameNode单点故障,难以应用于在线场景 NameNode压力过大 ...

  5. 亚马逊EC2服务器登录方法

    1.根据官网提供的方法登录连接到EC2服务器(官网推荐windows用户使用PUTTY连接) 2. 创建root的密码,输入如下命令: sudo passwd root 3.然后会提示你输入new p ...

  6. CreateProcess 执行CMD命令,并重定向输出

    1. 参考网址:http://www.cnblogs.com/cnarg/archive/2011/02/20/1959292.html function TfrmMain.ExecDosCmd :b ...

  7. ajax如何处理返回的数据格式是xml的情况

    <!DOCTYPE html> <html> <head> <title>用户注册</title> <meta charset=&qu ...

  8. Eclipse 下配置MySql5.6的连接池,使用Tomcat7.0

    目前找到的最简单的配置方法.   1.首先在eclipse中创建一个Dynamical Web Application,在WebContent文件夹下的META-INF文件夹中创建新的名为conten ...

  9. Druid:一个用于大数据实时处理的开源分布式系统——大数据实时查询和分析的高容错、高性能开源分布式系统

    转自:http://www.36dsj.com/archives/28590 Druid 是一个用于大数据实时查询和分析的高容错.高性能开源分布式系统,旨在快速处理大规模的数据,并能够实现快速查询和分 ...

  10. 批量修改mysql的表前缀

    如何批量修改mysql的表前缀名称教程中将涉及两种方法修改,一种是批量修改(使用php脚步完成)一种是精确修改(使用sql查询语句完成). 方法一:使用sql语句修改mysql数据库表前缀名 首先我们 ...