资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Angular父子组件间怎么传值

小编给大家分享一下Angular父子组件间怎么传值,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

分宜ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:028-86922220(备注:SSL证书合作)期待与您的合作!

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

这是父组件

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

{{transData}}

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 效果图

Angular父子组件间怎么传值

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

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

这是父组件

{{receiceData}}

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父子组件间怎么传值

以上是“Angular父子组件间怎么传值”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!


分享题目:Angular父子组件间怎么传值
文章起源:http://cdkjz.cn/article/ihojsc.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220