美文网首页web前端
angular组件通信

angular组件通信

作者: 姜治宇 | 来源:发表于2022-06-24 11:35 被阅读0次

拆分组件是一项必须的编程技能,符合高内聚低耦合的设计理念。

父子组件通信

父组件

father.html:

<div class="cont">
    <button (click)="handleSceneZip()"></button>
    ......
</div>
<!--子组件-->
<app-child #imszip [id]="id" (removeloadingevt)="cancelLoading($event)"></app-child>

father.ts:

import { Component, Inject, OnDestroy, OnInit, ViewChild } from '@angular/core';
@Component({
    selector: 'app-father',
    templateUrl: './father.component.html',
    styleUrls: ['./father.component.scss']
})
export class FatherComponent implements OnInit, OnDestroy {
    @ViewChild('imszip') imszip;//子组件
    id: number;
    imploading: boolean;
    constructor() {
        this.id = 123;
        this.imploading = false;
    }
    cancelLoading(evt) {//父组件接受子组件的反馈信息
        console.log('父组件接受的信息>>>', evt);
        if (evt === 'ok') {
            this.imploading = false;

        } else {
            this.imploading = true;
        }

    }
    handleSceneZip() {
        this.imszip.doSceneZip();//调用子组件的方法
    }
}
子组件

child.ts:

import { Component, OnInit, OnDestroy, ChangeDetectorRef,Input,Output,EventEmitter } from '@angular/core';

@Component({
    selector: 'app-child',
    templateUrl: './child.component.html',
    styleUrls: ['./child.component.scss']
})
export class ChildComponent implements OnInit, OnDestroy {
    @Input() id:number;//接收父组件的属性信息
    @Output() private removeloadingevt = new EventEmitter();//反馈到父组件的信息,名字跟父组件的方法名一致
    constructor() {

    }
    doSceneZip(){
        console.log('子组件实现的逻辑');
    }
    doSth(){
        this.removeloadingevt.emit('ok');//发送给父组件
    }
}

组件间通信

如果是没有关系的组件之间通信(比如兄弟组件),我们可以建一个公用的service,通过共享subject的方式实现通信。

service
import { Injectable } from "@angular/core";
import { Subject } from 'rxjs';
@Injectable({
    providedIn: 'root'
})
export class CommonshareService {
    public exportSceneSub$ = new Subject();//用于两个组件之间的信息共享
    constructor() { }
}
A组件
import { Component, OnInit, OnDestroy} from '@angular/core';
import { CommonshareService } from 'src/app/service/commonshare.service';
@Component({
    selector: 'app-a',
    templateUrl: '.a.component.html',
    styleUrls: ['./a.component.scss']
})
export class AComponent implements OnInit, OnDestroy {
   
   
    constructor(public commonshareService: CommonshareService) {

    }

    doSth(){
        this.commonshareService.next({id:123});//发布
    }
}
B组件
import { Component, OnInit, OnDestroy} from '@angular/core';
import { CommonshareService } from 'src/app/service/commonshare.service';
@Component({
    selector: 'app-b',
    templateUrl: '.a.component.html',
    styleUrls: ['./a.component.scss']
})
export class BComponent implements OnInit, OnDestroy {
   
    public exsceneSub: any;
    constructor(public commonshareService: CommonshareService) {
        this.exsceneSub = this.commonshareService.exportSceneSub$.subscribe(scene => {
            if (scene) {
                this.doSth(scene);
            }
        });
    }
    doSth(scene){
        console.log('做一些事情');
    }

    ngOnDestroy(): void {

        this.exsceneSub.unsubscribe(); //释放句柄
    }
}

相关文章

  • angular4 子组件向父组件通信

    之前我写过 父组件向子组件通信 :《angular4 父组件向子组件通信传值》https://www.jiansh...

  • Angular 4 事件冒泡

    Angular 组件和 DOM 元素通过事件与外部进行通信, Angular 事件绑定语法对于组件和 DOM 元素...

  • Vue 组件 / 通信

    子组件=》父组件 vue的组件之间的通信类似angular的父子层级之间通信,父组件获取子组件数据步骤大概如下: ...

  • angular 组件通信

    angular组件通信是很长常见的功能,现在总结下,常见通信主要用一下三种 父组件 => 子组件 子组件 => 父...

  • angular 组件通信

    原文链接 https://segmentfault.com/a/1190000008959575#articleH...

  • angular组件通信

    官网:https://angular.cn/guide/component-interaction[https:/...

  • angular组件通信

    拆分组件是一项必须的编程技能,符合高内聚低耦合的设计理念。 父子组件通信 父组件 father.html: fat...

  • 初识Angular2的组件通信

    Angular2中免不了进行父子组件的通信,现在就稍微的了解一下父组件怎么给子组件传值,首先,通过Angular-...

  • 自己总结的Angular2组建通信

    组件之间的通信 这里简单的记录自己在angular2中,使用组件通信的一些方法。方便自己以后的使用。 一、组件之间...

  • Angular 组件通信 5种方法

    组件化是Angular的核心概念,所以组件通信的使用就比较多而且很重要。 官方传送门?:https://angul...

网友评论

    本文标题:angular组件通信

    本文链接:https://www.haomeiwen.com/subject/ojmvvrtx.html