首页 > web前端 > js教程 > 正文

Angular父子组件间怎么进行通信?父子传值的方式浅析

青灯夜游
发布: 2021-08-10 10:02:07
转载
2017人浏览过

angular父子组件间怎么进行通信?本篇文章给大家介绍一下angular父子组件传值方式。

Angular父子组件间怎么进行通信?父子传值的方式浅析

通过Input和Ouput传值

父组件:html和ts

<app-liftcycle [name]="name" (changeName)="changeName($event)"></app-liftcycle>
登录后复制
public name: string = "jack";
public changeName(value: string) {
    this.name = value;
}
登录后复制

子组件:html和ts

<div (click)="emit()">{{name}}</div>
登录后复制
import { Component, Input, EventEmitter, Output } from '@angular/core';
@Input() name: string;
@Output() changeName: EventEmitter<string> = new EventEmitter<string>();
public emit() {
    this.changeName.emit("修改name属性");
}
登录后复制

【相关教程推荐:《angular教程》】

通过setter监听属性的变化

父组件同上,子组件:

private _name: string = "";
@Input() 
public get name(): string {
    return this._name;
}
public set name(value: string) {
    this._name = value + "定义结构";
}
登录后复制

通过ngOnChanges钩子函数监听输入属性的变化

ngOnChanges在监听多个属性的时候,要比setter的方式简便一些。

@Input() name: string;
ngOnChanges(changes: SimpleChanges): void {
    (({name}) => {
        console.log(name.currentValue,name.previousValue);
    })(changes);
}
登录后复制

父组件html中通过模板变量调用子组件的方法和属性。

通义万相
通义万相

通义万相,一个不断进化的AI艺术创作大模型

通义万相596
查看详情 通义万相

模板变量获取了子组件的一个引用。 父组件:

<app-liftcycle #child></app-liftcycle>
<button (click)="child.childFn()">按钮</button>
登录后复制

子组件:

public childFn() {
    console.log("通过模板变量调用子组件中的方法");
}
登录后复制

父组件通过ViewChild获取子组件实例

<app-liftcycle [name]="name" (changeName)="changeName($event)" #child></app-liftcycle>
<button (click)="childFn()">childFn</button>
登录后复制
@ViewChild("child") child: LiftcycleComponent;
public childFn(): void {
    this.child.childFn();
}
登录后复制

通过service进行通信

service:

import { Subject } from 'rxjs';
import { Injectable } from '@angular/core';

@Injectable({
    providedIn: 'root'
})
export class CommunService {

    constructor() {}
    public commun = new Subject<string>();
    communSend() {
        this.commun.next("send");
    }
}
登录后复制

父组件:

constructor(private commun: CommunService) { }
public send(): void {
    this.commun.communSend();
}
登录后复制

子组件:

constructor(private commun: CommunService) { 
    this.commun.commun.subscribe((value) => {console.log(value)});
}
登录后复制

父组件传递方法

父组件通过属性传递给子组件方法,子组件进行调用,一般不推荐,React采用这种通信方式。 可能是基于this的绑定错综复杂,所以angular不太推荐。React Hooks的出现也有一部分原因 是class类的this错综复杂。 父组件:

<app-liftcycle [send]="send.bind(this)"></app-liftcycle>
登录后复制
public name: string = "jack";
public send(): void {
    console.log(this.name);
}
登录后复制

子组件:

<button (click)="childSend()">childSend</button>
登录后复制
@Input() send: Function;
public childSend() {
    this.send();
}
登录后复制

更多编程相关知识,请访问:编程视频!!

以上就是Angular父子组件间怎么进行通信?父子传值的方式浅析的详细内容,更多请关注php中文网其它相关文章!

相关标签:
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:掘金社区网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号