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


本文摘自PHP中文网,作者青灯夜游,侵删。

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

相关推荐:《angular教程》

Angular中父子组件传值

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

1. 父组件给子组件传值

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

1.1 父组件hero-parent

1、hero-parent.component.html

1

2

<p>这是父组件</p>

<app-hero-child [transData]='tran_childData'></app-hero-child>

2、hero-parent.component.ts

1

2

3

4

5

6

7

8

9

10

11

12

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

1

<p>{{transData}}</p>

2、hero-child.component.ts

1

2

3

4

5

6

7

8

9

10

11

12

13

14

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. 子组件给父组件传递参数

  • 说明:子组件给父组件传递参数的时候需要导入OutputEventEmitter,引入模块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

    1

    <button (click)='transData_to_parent()'>我是子组件,给父组件传递参数</button>

  2. hero-child.component.ts

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    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

1

2

3

<p>这是父组件</p>

<p>{{receiceData}}</p>

<app-hero-child (childEvent)='receive_child_data($event)'></app-hero-child>

2、hero-parent.component.ts

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

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父子组件间如何传值?的详细内容,更多文章请关注木庄网络博客

相关阅读 >>

浅谈一下Angular模板引擎ng-template

浅谈Angular中组件(@component)基本知识

深入了解Angular中的component组件

聊聊Angular中的模板输入变量(let-变量)

AngularAngularjs的简单比较

深入了解Angular组件中的生命周期钩子

浅谈Angular cli的两种安装方式

浅谈Angular中的变化检测(change detection)

浅谈Angular中组件的生命周期

详解Angular中的ngmodule(模块)

更多相关阅读请进入《Angular》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...