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

本篇文章给大家介绍一下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

这是父组件

登录后复制

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

在这里插入图片描述

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

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

2.1 子组件hero-child

hero-child.component.html

登录后复制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父子组件间如何传值?的详细内容,更多请关注【创想鸟】其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2716815.html

(0)
上一篇 2025年3月7日 21:58:59
下一篇 2025年3月1日 16:49:38

AD推荐 黄金广告位招租... 更多推荐

相关推荐

  • 详解Angular根模块与特性模块

    本篇文章带大家了解一下angular中的特性模板和根模板。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。 前提是安装了 Angular cli,以下的大部分文件创建都是依赖于cli提供的指令 Angular中的特性模板和根…

    2025年3月7日 编程技术
    200
  • 浅谈Angular CLI的两种安装方式

    本篇文章给大家介绍一下angular cli的两种安装方式:在线安装和离线安装。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。 相关教程推荐:《angular教程》 Angular CLI 安装方式 默认已经安装了 Nod…

    2025年3月7日
    200
  • 详解Angular中的Observable(可观察对象)

    本篇文章带大家了解一下angular 可观察对象(observable)。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。 相关教程推荐:《angular教程》 可观察对象(Observable) 可观察对象支持在应用的发布…

    2025年3月7日
    200
  • 浅谈Angular组件的交互方式

    本篇文章和大家一起聊聊angular组件的交互方式。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。 Angular 组件交互 组件交互: 组件通讯,让两个或多个组件之间共享信息。 使用场景: 当某个功能在多个组件中被使用到…

    2025年3月7日
    200
  • 13个关于angular的前端面试题(总结)

    本篇文章给大家总结分享13个关于angular的前端面试题。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。 1,ng-if 跟 ng-show/hide 的区别有哪些? 第一点区别是,ng-if 在后面表达式为 true …

    2025年3月7日
    200
  • Angular怎么刷新当前页面?方法介绍

    本篇文章给大家分享几种angular刷新当前页面的方法。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。 Angular刷新当前页面的几种方法 默认,当收到导航到当前URL的请求,Angular路由器会忽略。 Heroes …

    2025年3月7日
    200
  • 深入浅析Angular中Directive的用法

    本篇文章给大家详细介绍一下angular directive,了解它的用法。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。 Angular Directive 学习 学习目的:为了更好的了解 ng directive 的使…

    2025年3月7日
    200
  • 20个优秀的Angular开源项目,你了解几个呢?

    本篇文章给大家分享20个你值得了解的angular开源项目。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。 相关教程推荐:《angular教程》 1.Angular-CLI – angular工具命令行 Git…

    2025年3月7日 编程技术
    200
  • 详解Angular中的Route路由

    本篇文章带大家一起了解angular中的路由(route)。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。 Angular 路由(Route) 我们可以将路由器理解成控制整个应用状态的视图对象, 每个应用都有一个路由器; …

    2025年3月7日
    200
  • 详解Angular中的NgModule(模块)

    本篇文章带大家详细了解一下angular中的ngmodule(模块)。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。 Angular 模块(NgModule) Angular 应用是模块化的, 它拥有自己的模块化系统, 称…

    2025年3月7日
    200

发表回复

登录后才能评论