登录  /  注册
首页 > web前端 > js教程 > 正文

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

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

本篇文章给大家介绍一下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></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>我是子组件,给父组件传递参数</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></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中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:csdn网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 //m.sbmmt.com/ All Rights Reserved | php.cn | 湘ICP备2023035733号