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

详解Angular父子组件间如何传值?

青灯夜游
发布: 2021-03-22 10:19:09
转载
2594人浏览过

本篇文章给大家介绍一下angular中父子组件间传值的方法。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。

详解Angular父子组件间如何传值?

相关推荐:《angular教程

Angular中父子组件传值

官方地址:https://angular.cn/guide/component-interaction#component-interaction

1. 父组件给子组件传值

  • 说明: 父组件给子组件传值的时候,父组件中在子组件的选择器上绑定数据即可
  • 子组件接收的时候需要引入input模块import { Component, OnInit, Input} from '@angular/core'
  • 子组件还需要使用语法糖的形式接收父组件传递的参数@input() transData

1.1 父组件hero-parent

1、hero-parent.component.html

<p>这是父组件</p>
<app-hero-child [transData]='tran_childData'></app-hero-child>
登录后复制

2、hero-parent.component.ts

import { Component, OnInit } from '@angular/core'

@Component({
    selector: 'app-hero-parent',
    templateUrl: './app-hero-parent.component.html',
    styleUrls: ['./app-hero-parent.component.scss']
})
export class HeroesComponent implements OnInit {
    tran_childData:string = '这是父组件传递给子组件的数据'
    constructor() {}
    ngOnInit(): void {}
}
登录后复制
1.2 子组件hero-child

1、hero-child.component.html

<p>{{transData}}</p>
登录后复制

2、hero-child.component.ts

import { Component, OnInit, Input} from '@angular/core'

@Component({
    selector: 'app-hero-child',
    templateUrl: './app-hero-child.component.html',
    styleUrls: ['./app-hero-child.component.scss']
})
export class DetailComponent implements OnInit {
    @Input() transData: string
    constructor() {}
    ngOnInit(): void {
        console.log(this.transData)
    }
}
登录后复制
1.3 效果图

在这里插入图片描述

2. 子组件给父组件传递参数

  • 说明:子组件给父组件传递参数的时候需要导入Output和EventEmitter,引入模块import { Component, OnInit, Output, EventEmitter} from '@angular/core'
  • 使用的时候需要先暴露一个方法@Output() childEvent = new EventEmitter()用来使用emit传递数据
  • 具体使用this.childEvent.emit('我是子组件传递的数据')

2.1 子组件hero-child

  1. hero-child.component.html
    <button (click)='transData_to_parent()'>我是子组件,给父组件传递参数</button>
    登录后复制
  2. hero-child.component.ts
    import { Component, OnInit, Output, EventEmitter} from '@angular/core'
    
    @Component({
        selector: 'app-hero-child',
        templateUrl: './app-hero-child.component.html',
        styleUrls: ['./app-hero-child.component.scss']
    })
    export class DetailComponent implements OnInit {
        @Output() childEvent = new EventEmitter()
        constructor() {}
        ngOnInit(): void {},
        // 给父组件传递参数
        transData_to_parent() {
            this.childEvent.emit('我是子组件传递的数据')
        }
    }
    登录后复制

2.2 父组件hero-parent

1、hero-parent.component.html

<p>这是父组件</p>
<p>{{receiceData}}</p>
<app-hero-child (childEvent)='receive_child_data($event)'></app-hero-child>
登录后复制

2、hero-parent.component.ts

import { Component, OnInit } from '@angular/core'
	
@Component({
    selector: 'app-hero-parent',
    templateUrl: './app-hero-parent.component.html',
    styleUrls: ['./app-hero-parent.component.scss']
})
export class HeroesComponent implements OnInit {
    constructor() {}
    ngOnInit(): void {}
    receiceData:string
    // 接收子组件传递的数据
    receive_child_data(data) {
        this.receiceData = data
    }
}
登录后复制

2.3 效果图

在这里插入图片描述

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

以上就是详解Angular父子组件间如何传值?的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

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

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

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