• 技术文章 >web前端 >js教程

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

    青灯夜游青灯夜游2021-03-22 18:44:00转载530
    本篇文章给大家介绍一下Angular中父子组件间传值的方法。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。

    相关推荐:《angular教程

    Angular中父子组件传值

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

    1. 父组件给子组件传值

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

    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中文网其它相关文章!

    声明:本文转载于:csdn,如有侵犯,请联系admin@php.cn删除
    上一篇:浅谈Nodejs中内置模块的基本用法 下一篇:深入理解JavaScript立即调用函数表达式(IIFE)
    大前端线上培训班

    相关文章推荐

    • angular8如何封装http服务• 浅谈Angular10配置@路径别名的方法• 浅谈Angular中插槽的用法• 浅谈angular9中路由守卫的用法• 浅谈angular9中组件动态加载的实现方法

    全部评论我要评论

  • 取消发布评论发送
  • 1/1

    PHP中文网