Angular中组件之间的传值

Angular中组件之间的传值


文章目录

  • Angular中组件之间的传值
    • 前言
    • 一、父亲向儿子传值
    • 二、儿子向父亲传值
    • 三、爷爷向孙子传值
    • 四、兄弟之间的传值


前言

Angular的组件是构成应用的基础单元,它们封装了HTML模板、TypeScript代码以及CSS样式,以实现特定的功能。组件的目的是为了复用和减少代码的重复度。

以下是对Angular组件的详细介绍:

  1. 组件的组成:
    HTML模板(Template):定义了组件的视图结构,用于展示数据并响应用户交互。
    TypeScript代码(Script):包含组件的类定义、业务逻辑以及数据模型。这个类通常使用@Component()装饰器进行标记,装饰器中包含了组件的元数据,如选择器(Selector)、模板URL等。
    CSS样式(Style):定义了组件的外观和样式。

  2. 组件的创建:
    使用Angular CLI(命令行界面)可以快速创建组件。通过执行ng generate component 组件名命令,Angular CLI会自动生成组件所需的文件,包括.ts(TypeScript代码文件)、.html(HTML模板文件)和.css(CSS样式文件)。
    手动创建组件时,需要分别创建这三个文件,并在TypeScript代码文件中使用@Component()装饰器来定义组件的元数据。

  3. 组件的核心任务:
    将数据渲染到视图:组件负责将数据模型中的数据渲染到HTML模板中,以便在视图中展示给用户。
    监听用户在视图上的操作:组件可以监听用户在视图上的各种操作(如点击、输入等),并执行相应的业务逻辑。

  4. 组件的复用:
    Angular组件具有很好的复用性。一旦创建了一个组件,就可以在其他组件或应用程序中重复使用它,从而减少了代码的重复度,提高了开发效率。

  5. 依赖注入:
    Angular组件可以通过依赖注入(Dependency Injection)机制来满足其执行功能所需的服务或对象。这使得组件可以更加灵活和可配置。

总的来说,Angular组件是构建复杂Web应用程序的关键部分。通过创建可复用的组件,开发人员可以更加高效地构建和维护代码库,并减少错误和重复工作。

一、父亲向儿子传值

首先使用命令创建两个组件,一个parent一个child

ng g component components/parent
ng g component components/child

@Input(): 用于从父组件接收数据。当你想在组件之间传递数据时,可以在子组件中使用@Input()装饰器来定义一个输入属性。父组件可以通过这个属性将数据传递给子组件。

在这里插入图片描述

parent.component.ts

import { Component, OnInit } from '@angular/core';
import { ChildComponent } from "../child/child.component";

@Component({
    selector: 'app-parent',
    standalone: true,
    templateUrl: './parent.component.html',
    styleUrl: './parent.component.css',
    imports: [ChildComponent]
})
export class ParentComponent  {
   value = 'parent的value';
}

parent.component.html

<p>parent works!</p>

<app-child [childValue]="value"></app-child>

<p>{{ value }}</p>

<p>parent works end!</p>

child.component.ts

import { Component, Input, EventEmitter , Output } from '@angular/core';

@Component({
  selector: 'app-child',
  standalone: true,
  imports: [],
  templateUrl: './child.component.html',
  styleUrl: './child.component.css'
})
export class ChildComponent {
  @Input() childValue:string = "Hello, I am child component.";
}

child.component.html

<p>child works!</p>

<p>{{childValue}}</p>

<p>child works end!</p>

最后在app.component.ts中引入ParentComponent

import { Component } from '@angular/core';
import { RouterOutlet } from '@angular/router';

import { ParentComponent } from './components/parent/parent.component';

@Component({
  selector: 'app-root',
  standalone: true,
  imports: [RouterOutlet,ParentComponent],
  templateUrl: './app.component.html',
  styleUrl: './app.component.css'
})
export class AppComponent {
  title = 'first-component';
}

app.component.html中使用组件

<p>这个是app</p>

<app-parent></app-parent>

运行效果

在这里插入图片描述

二、儿子向父亲传值

@Output(): 用于从子组件发送事件到父组件。你可以使用@Output()装饰器来定义一个输出属性,并通过这个属性发出事件。父组件可以监听这个事件并作出响应。

child.component.ts

import { Component, Input, EventEmitter , Output } from '@angular/core';

@Component({
  selector: 'app-child',
  standalone: true,
  imports: [],
  templateUrl: './child.component.html',
  styleUrl: './child.component.css'
})
export class ChildComponent {

  @Input() childValue:string = "Hello, I am child component.";

  @Output() valueSend = new EventEmitter<string>();

  sendValue() {
    this.valueSend.emit('这是child组件发送的值');
  }
}

child.component.html

<p>child works!</p>

<p>{{childValue}}</p>

<button (click)="sendValue()">传给父组件</button>

<p>child works end!</p>

parent.component.ts

import { Component, OnInit } from '@angular/core';
import { ChildComponent } from "../child/child.component";

@Component({
    selector: 'app-parent',
    standalone: true,
    templateUrl: './parent.component.html',
    styleUrl: './parent.component.css',
    imports: [ChildComponent]
})
export class ParentComponent  {

   value = 'parent的value';
   iCount =1;

   receiveValue(value: string) {

    this.value=value + "" + this.iCount++;
    console.log(value); 
  }
}

parent.component.html

<p>parent works!</p>

<app-child [childValue]="value" (valueSend)="receiveValue($event)"></app-child>

<p>{{ value }}</p>

<p>parent works end!</p>

运行效果

在这里插入图片描述

点击按钮

在这里插入图片描述

在这里插入图片描述

三、爷爷向孙子传值

grandfather.component.ts

import { Component } from '@angular/core';
import { ParentComponent } from "../parent/parent.component";

@Component({
    selector: 'app-grandfather',
    standalone: true,
    templateUrl: './grandfather.component.html',
    styleUrl: './grandfather.component.css',
    imports: [ParentComponent]
})
export class GrandfatherComponent {
  grandfatherVaule = '爷爷组件的值';
}

grandfather.component.html

<p>grandfather works!</p>

<app-parent [grandparentValue]="grandfatherVaule"></app-parent>

<p>grandfather works end!</p>

parent.component.ts

import { Component, Input, OnInit } from '@angular/core';
import { ChildComponent } from "../child/child.component";

@Component({
    selector: 'app-parent',
    standalone: true,
    templateUrl: './parent.component.html',
    styleUrl: './parent.component.css',
    imports: [ChildComponent]
})
export class ParentComponent  {

   value = 'parent的value';
   iCount =1;

   @Input() grandparentValue: string = ''; // 假设这个值是从爷爷组件传入的


   receiveValue(value: string) {

    this.value=value + "" + this.iCount++;
    console.log(value); 
  }

}

parent.component.html

<p>parent works!</p>
<app-child [parentValue]="grandparentValue"></app-child>
<p>{{ value }}</p>
<p>parent works end!</p>

child.component.ts

import { Component, Input, EventEmitter , Output } from '@angular/core';

@Component({
  selector: 'app-child',
  standalone: true,
  imports: [],
  templateUrl: './child.component.html',
  styleUrl: './child.component.css'
})
export class ChildComponent {

  @Input() childValue:string = "Hello, I am child component.";

  @Output() valueSend = new EventEmitter<string>();

  @Input() parentValue: string='';
  sendValue() {
    this.valueSend.emit('这是child组件发送的值');
  }
}

child.component.html

<p>child works!</p>

<p>{{childValue}}</p>

<button (click)="sendValue()">传给父组件</button>

<p>{{parentValue}}</p>

<p>child works end!</p>

运行效果

在这里插入图片描述

四、兄弟之间的传值

同样创建三个组件

在这里插入图片描述

brother-father.component.ts

import { Component } from '@angular/core';
import { BrotherOneComponent } from "../brother-one/brother-one.component";
import { BrotherTwoComponent } from "../brother-two/brother-two.component";

@Component({
    selector: 'app-brother-father',
    standalone: true,
    templateUrl: './brother-father.component.html',
    styleUrl: './brother-father.component.css',
    imports: [BrotherOneComponent, BrotherTwoComponent]
})
export class BrotherFatherComponent {
  receiveValue(value: string) {
    console.log(value); // 将打印 "Value from Child"
  }
  sharedValue = 'Shared value between brother';
}

brother-father.component.html

<p>brother-father works!</p>

<app-brother-one [sharedValue]="sharedValue"></app-brother-one>
<app-brother-two [sharedValue]="sharedValue"></app-brother-two>

<p>brother-father works end!</p>

brother-one.component.ts

import { Component, Input } from '@angular/core';

@Component({
  selector: 'app-brother-one',
  standalone: true,
  imports: [],
  templateUrl: './brother-one.component.html',
  styleUrl: './brother-one.component.css'
})
export class BrotherOneComponent {
  @Input() sharedValue: string='';
}

brother-one.component.html

<p>brother-one works!</p>

<p>brother-one的值为{{sharedValue}}</p>

<p>brother-one works end!</p>

brother-two.component.ts

import { Component,Input } from '@angular/core';

@Component({
  selector: 'app-brother-two',
  standalone: true,
  imports: [],
  templateUrl: './brother-two.component.html',
  styleUrl: './brother-two.component.css'
})
export class BrotherTwoComponent {
  @Input() sharedValue: string='';
}

brother-two.component.html

<p>brother-two works!</p>

<p>brother-two的值为{{sharedValue}}</p>

<p>brother-two works end!</p>

app.component.ts

import { Component } from '@angular/core';
import { RouterOutlet } from '@angular/router';
import { ParentComponent } from './components/parent/parent.component';
import { GrandfatherComponent } from "./components/grandfather/grandfather.component";
import { BrotherFatherComponent } from './components/brother-father/brother-father.component';

@Component({
    selector: 'app-root',
    standalone: true,
    templateUrl: './app.component.html',
    styleUrl: './app.component.css',
    imports: [RouterOutlet, ParentComponent, GrandfatherComponent,BrotherFatherComponent]
})
export class AppComponent {
  title = 'first-component';
}

parent.component.html

<p>parent works!</p>

<app-child [parentValue]="grandparentValue"></app-child>

<p>{{ value }}</p>

<p>parent works end!</p>

运行效果

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/598249.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

【算法学习】day2

文章目录 BFS1.图像渲染2.岛屿数量 BFS 1.图像渲染 思路&#xff1a;BFS宽度遍历&#xff0c;我们需要对初始像素进行一层一层遍历&#xff0c;也就是上下左右四个方向进行遍历判断&#xff0c;如何访问这四个方向呢&#xff0c;就需要利用两个数组dx和dy来进行判断和遍历&…

【RPC】Dubbo接口测试

关于rpc&#xff0c;推荐看看这篇 &#xff1a; 既然有HTTP协议&#xff0c;为什么还要有RPC 一、Dubbo 是一款alibaba开源的高性能服务框架&#xff1a; 分布式服务框架高性能和透明化的RPC远程服务调用方案SOA服务治理方案 二、Dubbo基础架构 三、 Dubbo接口测试 1、jme…

毕业设计参考-PyQt5-YOLOv8-鱼头鱼尾鱼长测量程序,OpenCV、Modbus通信、YOLO目标检测综合应用

“PyQt5-YOLOv8-鱼头鱼尾鱼长测量程序”是一个特定的软件程序&#xff0c;用于通过图像处理和目标检测技术来测量鱼类的长度。 视频效果&#xff1a; 【毕业设计】基于yolo算法与传统机器视觉的鱼头鱼尾识别_哔哩哔哩_bilibili 这个程序结合了多种技术&#xff1a; 1. OpenCV…

并行执行的概念—— 《OceanBase 并行执行》系列 一

From 产品经理&#xff1a; 这是一份姗姗来迟的关于OceanBase并行执行的系统化产品文档。 自2019年起&#xff0c;并行执行功能已被许多客户应用于多种场景之中&#xff0c;其重要性日益凸显。然而&#xff0c;遗憾的是&#xff0c;我们始终未能提供一份详尽的用户使用文档&…

如何应对访问国外服务器缓慢的问题?SDWAN组网是性价比之选

访问国外服务器缓慢通常由以下原因造成&#xff1a; 1、政策限制&#xff1a;我国管理互联网&#xff0c;限制部分国外网站和服务器&#xff0c;以维护国家安全稳定。 2、技术障碍&#xff1a;国内与国际互联网的网络架构和协议存在差异&#xff0c;可能导致数据传输不兼容。 …

探索AI编程新纪元:从零开始的智能编程之旅

提示&#xff1a;Baidu Comate 智能编码助手是基于文心大模型&#xff0c;打造的新一代编码辅助工具 文章目录 前言AI编程概述&#xff1a;未来已来场景需求&#xff1a;从简单到复杂&#xff0c;无所不包体验步骤&#xff1a;我的AI编程初探试用感受&#xff1a;双刃剑下的深思…

docker资源限额

多数的应⽤场景要对Docker容器的运⾏内存进⾏限制&#xff0c;防⽌其使⽤过多的内存。 格式&#xff1a;-m或--memory 正常的内存大小 [rootadmin ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS …

“40法则”视角下的中国网络安全公司

“40法则”视角下国内网安上市公司2023年业绩表现 采用“40法则”衡量&#xff0c;首先需要考虑的是营收增长和利润水平的衡量指标&#xff0c;在上一篇文章中已经详细说明&#xff0c;在此不再赘述。 增长速度的衡量指标&#xff0c;可以选择公司的营业收入的同比增长率。 …

华为OD机试 - 掌握的单词个数 - 回溯(Java 2024 C卷 100分)

华为OD机试 2024C卷题库疯狂收录中&#xff0c;刷题点这里 专栏导读 本专栏收录于《华为OD机试&#xff08;JAVA&#xff09;真题&#xff08;A卷B卷C卷&#xff09;》。 刷的越多&#xff0c;抽中的概率越大&#xff0c;每一题都有详细的答题思路、详细的代码注释、样例测试…

怎么将pdf的文件内容保存到mysql数据库中?

要将PDF导入到MYSQL&#xff0c;首先一步就是要先将PDF内容结构化&#xff0c;如果其内容为非结构化&#xff0c;则导入MYSQL的意义不大&#xff0c;具体操作方法如下&#xff1a; 将PDF文件的内容保存到MySQL数据库中通常涉及几个步骤。PDF文件包含的是格式化文本、图像和其他…

​XMall商城微信小程序前端技术解析

摘要 随着移动互联网的深入发展&#xff0c;微信小程序以其轻量级、便捷性和即用即走的特点&#xff0c;成为了众多企业和开发者关注的焦点。XMall商城微信小程序前端作为一款开源项目&#xff0c;以其精美的页面设计、丰富的功能和高效的性能&#xff0c;受到了广大开发者和用…

深度学习之基于Matlab BP神经网络烟叶成熟度分类

欢迎大家点赞、收藏、关注、评论啦 &#xff0c;由于篇幅有限&#xff0c;只展示了部分核心代码。 文章目录 一项目简介 二、功能三、系统四. 总结 一项目简介 一、项目背景 烟叶的成熟度是评估烟叶品质的重要指标之一&#xff0c;它直接影响着烟叶的口感、香气和理化特性。传…

还不懂 RESTful 接口是什么?快进来看看

RESTful是指基于REST&#xff08;Representational State Transfer&#xff0c;表现层状态转移&#xff09;架构风格的Web服务。REST是一种设计原则和架构风格&#xff0c;而不是标准&#xff0c;它用于指导如何构建易于交互、高效、可扩展的网络系统。RESTful服务通常使用HTTP…

Oracle Database 23ai Free RPM Installation On Oracle Linux 8 (OL8)

Oracle刚刚发布了最新的Oracle database 23ai版本测试安装包&#xff0c;有兴趣的小伙伴可以安装体验一下。 关于安装的介质可以去如下地址下载&#xff1a; Oracle linux 8.9 Oracle Linux ISOs | Oracle, Software. Hardware. Complete. Oracle database 23ai安装包 Get Star…

Read timed out. (python 安装第三方库超时)

不少人在安装python第三方库的时候经常发生下面情况 解决方法就是往上找 我这里就是 jupyterlab-4.1.8-py3-none-any.whl安装时间过长&#xff0c;失败 那就去国内镜像网站下载下来离线安装 https://pypi.tuna.tsinghua.edu.cn/simple/xxx&#xff08;xxx就是你的包名&#…

AI绘画Stable Diffusion【艺术写真】:冰雪奇缘,使用ReActor插件实现AI写真

大家好&#xff0c;我是设计师阿威。 前面分享过几篇使用AI绘画Stable DIffusion中的InstantID插件实现AI写真的制作方法。 目前换脸插件有很多&#xff0c;比较典型的有Roop,ReActor,IP-Adapter,InstantID&#xff0c;就目前效果来看&#xff0c;InstantID单张图像换脸的相似…

数据结构:时间复杂度/空间复杂度

目录 一、时间复杂度 定义 常见的时间复杂度 如何计算时间复杂度 计算方法 三、实例分析 二、空间复杂度 定义 重要性 常见的空间复杂度 二、空间复杂度 定义 重要性 常见的空间复杂度 计算方法 三、实例分析 大O的渐进表示法 最好情况&#xff08;Best Case…

【前端】实现表格简单操作

简言 表格合并基础篇 本篇是在上一章的基础上实现&#xff0c;实现了的功能有添加行、删除行、逆向选区、取消合并功能。 功能实现 添加行 添加行分为在上面添加和在下面追加行。 利用 insertAdjacentElement 方法实现&#xff0c;该方法可以实现从前插入元素和从后插入元…

一起长锈:3 类型安全的Rust宏(从Java与C++转Rust之旅)

讲动人的故事,写懂人的代码 故事梗概:在她所维护的老旧Java系统即将被淘汰的危机边缘,这位在编程中总想快速完事的女程序员,希望能转岗到公司内部使用Rust语言的新项目组,因此开始自学Rust;然而,在掌握了Rust编程知识之后,为了通过Rust项目组的技术面试,使得转岗成功而…

【C语言】动态分配内存

内存的五大分区 1、堆区&#xff08;heap&#xff09;——由程序员分配和释放&#xff0c; 若程序员不释放&#xff0c;程序结束时一般由操作系统回收。注意它与数据结构中的堆是两回事 2、栈区&#xff08;stack&#xff09;——由编译器自动分配释放 &#xff0c;存放函数的…
最新文章