Angular@Input()详解与使用技巧
时间:2025-10-18 08:09:34 449浏览 收藏
本篇文章主要是结合我之前面试的各种经历和实战开发中遇到的问题解决经验整理的,希望这篇《Angular @Input() 使用全解析》对你有很大帮助!欢迎收藏,分享给更多的需要的朋友学习~

本文详细讲解了 Angular 中父组件向子组件传递数据的常用方法——@Input() 装饰器。通过一个图片展示的示例,我们将学习如何在父组件中定义数据,并将其传递到子组件中进行展示,同时避免一些常见的错误,确保数据正确加载和显示。
使用 @Input() 进行数据传递
在 Angular 应用中,组件化是核心概念之一。组件之间经常需要进行数据交互,其中一个常见场景就是父组件向子组件传递数据。@Input() 装饰器是实现这一功能的关键。
示例:图片展示组件
假设我们有一个父组件 AppComponent,它包含一个图片列表 images。我们希望将这些图片数据传递给子组件 CardComponent,并在子组件中展示这些图片。
1. 父组件 (AppComponent)
首先,在 AppComponent 中定义 images 数组:
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
})
export class AppComponent implements OnInit {
title = 'shopping-cart';
ngOnInit() {}
images = [
{
title: 'At the beach',
url: 'https://images.unsplash.com/photo-1507525428034-b723cf961d3e?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1173&q=80',
},
{
title: 'At the forest',
url: 'https://images.unsplash.com/photo-1448375240586-882707db888b?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1170&q=80',
},
{
title: 'At the City',
url: 'https://images.unsplash.com/photo-1449824913935-59a10b8d2000?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1170&q=80',
},
{
title: 'At the Snow',
url: 'https://images.unsplash.com/photo-1517299321609-52687d1bc55a?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1170&q=80',
},
{
title: 'At the beach',
url: 'https://images.unsplash.com/photo-1507525428034-b723cf961d3e?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1173&q=80',
},
{
title: 'At the forest',
url: 'https://images.unsplash.com/photo-1448375240586-882707db888b?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1170&q=80',
},
{
title: 'At the City',
url: 'https://images.unsplash.com/photo-1449824913935-59a10b8d2000?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1170&q=80',
},
{
title: 'At the Snow',
url: 'https://images.unsplash.com/photo-1517299321609-52687d1bc55a?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1170&q=80',
},
];
}接下来,在 AppComponent 的模板文件中,使用 *ngFor 循环遍历 images 数组,并将每个 image 对象传递给 CardComponent:
<app-nav></app-nav>
<div class="container">
<div class="columns mt-5 is-8 is-variable is-flex-wrap-wrap">
<div class="column is-4-tablet is-3-desktop" *ngFor="let image of images">
<app-card [image]="image"></app-card>
</div>
</div>
</div>注意 [image]="image" 这部分,它将父组件的 image 变量绑定到子组件 app-card 的 image 输入属性上。
2. 子组件 (CardComponent)
在 CardComponent 中,使用 @Input() 装饰器声明一个输入属性 image,用于接收父组件传递的数据:
import { Component, Input, OnInit } from '@angular/core';
@Component({
selector: 'app-card',
templateUrl: './card.component.html',
styleUrls: ['./card.component.css'],
})
export class CardComponent implements OnInit {
@Input() image: any = {};
constructor() {}
ngOnInit(): void {
console.log(this.image);
}
}@Input() image: any = {} 声明了一个名为 image 的输入属性,类型为 any,并设置了一个默认值空对象 {}。 建议明确指定类型,例如 Image 接口,提高代码可读性和可维护性。
最后,在 CardComponent 的模板文件中,使用 image 属性来展示图片:
<div *ngIf="image" class="card">
<div class="card-image">
<figure class="image is-4by3">
<img [src]="image.url" [alt]="image.title" />
</figure>
</div>
<footer class="card-footer">
<p class="card-footer-item">
<span>
View on
<a href="https://twitter.com/codinghorror/status/506010907021828096">Twitter</a>
</span>
</p>
<p class="card-footer-item">
<span> Share on <a href="#">Facebook</a> </span>
</p>
</footer>
<div>{{ image.title }}</div>
</div>关键点:
- *ngIf="image":在加载 image 数据之前,防止出现错误。
- [src]="image.url":使用 image.url 来绑定图片的 URL。
- [alt]="image.title": 使用 image.title 作为图片的 alt 属性,增强可访问性。
- {{ image.title }}: 显示图片的标题。
注意事项
- 属性命名: @Input() 装饰器后面的属性名(例如 image)应该与父组件模板中绑定的属性名([image])保持一致。
- 数据类型: 建议为 @Input() 属性指定明确的数据类型,例如接口或类,以提高代码的可读性和可维护性。
- 默认值: 为 @Input() 属性设置默认值可以避免在数据加载完成之前出现错误。
总结
通过 @Input() 装饰器,我们可以轻松地将数据从父组件传递到子组件。在实际开发中,理解和掌握 @Input() 的使用方法,能够帮助我们构建更加灵活和可维护的 Angular 应用。记住,清晰的属性命名、明确的数据类型和合适的默认值是保证数据传递成功的关键。
本篇关于《Angular@Input()详解与使用技巧》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于文章的相关知识,请关注golang学习网公众号!
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
349 收藏
-
398 收藏
-
410 收藏
-
280 收藏
-
297 收藏
-
476 收藏
-
142 收藏
-
179 收藏
-
122 收藏
-
404 收藏
-
201 收藏
-
182 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 485次学习