[Angular] 笔记 8:list/detail 页面以及@Input

1. list 页面

list/detail 是重要的 UI 设计模式。

vscode terminal 运行如下命令生成 detail 组件:

PS D:\Angular\my-app> ng generate component pokemon-base/pokemon-detail --module=pokemon-base/pokemon-base.module.ts
CREATE src/app/pokemon-base/pokemon-detail/pokemon-detail.component.html (29 bytes)
CREATE src/app/pokemon-base/pokemon-detail/pokemon-detail.component.spec.ts (649 bytes)
CREATE src/app/pokemon-base/pokemon-detail/pokemon-detail.component.ts (306 bytes)
CREATE src/app/pokemon-base/pokemon-detail/pokemon-detail.component.css (0 bytes)
UPDATE src/app/pokemon-base/pokemon-base.module.ts (428 bytes)

也可以使用通过安装 Angular Files 扩展生成上面的 detail 组件:

在这里插入图片描述

安装扩展后可以直接使用右键菜单命令生成组件:

在这里插入图片描述

工程文件结构:

在这里插入图片描述

然后修改 pokemon-base.module.ts:

@NgModule({declarations: [PokemonListComponent, PokemonDetailComponent],imports: [CommonModule],// 增加 PokemonDetailComponentexports: [PokemonListComponent, PokemonDetailComponent],
})
export class PokemonBaseModule {}

工程中现在有两个新生成的组件,pokemon-listpokemon-detail,其中,pokemon-list 是 smart 组件,pokemon-detail 是 dumm 组件。
smart 组件总是向下传递数据给 dumm 组件,smart 组件之所以称为 smart ,是因为它能从数据库获得数据,相对而言,dumm 组件不会访问数据库,它只能从 smart 组件那里接收数据。

接下来将 app.component.ts 中的数据移至更合适的地方,通常来说,app.component.ts 中直接存放数据不是最佳的设计方式。虽然在某些情况下可能需要在 app 组件中保留关键数据,但一般来说, app 中的其他代码能移则移,移到其他更合适的组件中,以提高代码的可维护性和可扩展性。

app.component.ts:

export class AppComponent {constructor() {}// 删除!!!// pokemons: Pokemon[] = [//   // Pokemon: 精灵宝可梦//   {//     id: 1,//     name: 'pikachu', // 皮卡丘//     type: 'electric',//     isCool: false,//     isStylish: true,//   },//   {//     id: 2,//     name: 'squirtle', // 杰尼龟//     type: 'water',//     isCool: true,//     isStylish: true,//   },//   {//     id: 3,//     name: 'charmander', // 小火龙//     type: 'fire',//     isCool: true,//     isStylish: false,//   },// ];
}

将数据粘贴到 pokemon-list.component.ts,模拟从数据库中获取的数据:

import { Component, OnInit } from '@angular/core';
@Component({selector: 'app-pokemon-list',templateUrl: './pokemon-list.component.html',styleUrls: ['./pokemon-list.component.css'],
})
export class PokemonListComponent implements OnInit {// 从 app.component.ts 里剪切过来的数据pokemons: Pokemon[] = [// pokemon: 精灵宝可梦{id: 1,name: 'pikachu', // 皮卡丘type: 'electric',isCool: false,isStylish: true,},{id: 2,name: 'squirtle', // 杰尼龟type: 'water',isCool: true,isStylish: true,},{id: 3,name: 'charmander', // 小火龙type: 'fire',isCool: true,isStylish: false,},];constructor() {}ngOnInit(): void {}
}

app文件夹下新建文件夹models 以及新文件 pokemon.ts, 将用于类型检查的 Pokemon interface 移到此文件中,以后项目中的其他类也可以使用此 interface,这样能够避免代码重复。

在这里插入图片描述

pokemon.ts,

export interface Pokemon {id: number;name: string;type: string;isCool: boolean;isStylish: boolean;
}

相应地,pokemon-list.component.ts 中增加 import { Pokemon } from 'src/app/models/pokemon'; 以使用此 interface.

pokemon-list.component.html:

<table><thead><th>Name</th><th>Index</th></thead><tbody><tr *ngFor="let pokemon of pokemons; let i = index"><td class="pokemon-td" [class.cool-bool]="pokemon.isCool">{{ i }} {{ pokemon.name }}</td></tr><tr *ngFor="let pokemon of pokemons; let i = index"><td class="pokemon-td" [ngClass]="{ 'cool-bool': pokemon.isCool }">{{ i }} {{ pokemon.name }}</td></tr><tr *ngFor="let pokemon of pokemons; let i = index"><tdclass="pokemon-td"[style.backgroundColor]="pokemon.isStylish ? '#800080' : ''">{{ i }} {{ pokemon.name }}</td></tr><tr *ngFor="let pokemon of pokemons; let i = index"><tdclass="pokemon-td"[ngStyle]="{ 'backgroundColor': (pokemon.isStylish ? '#800080' : '') }">{{ i }} {{ pokemon.name }}</td></tr></tbody></table>

运行 ng serve, 可看到如下界面:

在这里插入图片描述

2. detail 页面:

这一步要做的是迭代 pokemons 数组,将每一个 pokemon 传给 detail

在这里插入图片描述
修改 pokemon-detail.component.ts:

import { Component, Input, OnInit } from '@angular/core';
import { Pokemon } from 'src/app/models/pokemon';@Component({selector: 'app-pokemon-detail',templateUrl: './pokemon-detail.component.html',styleUrls: ['./pokemon-detail.component.css'],
})
export class PokemonDetailComponent implements OnInit {// 增加以下两行代码:@Input()detail!: Pokemon; // add a ! - a bang or null operator or null coalescingconstructor() {}ngOnInit(): void {}
}

重构 pokemon-list.component.html:

<table><thead><th>Name</th><th>Index</th></thead><tbody><app-pokemon-detail*ngFor="let pokemon of pokemons"[detail]="pokemon"></app-pokemon-detail></tbody>
</table>

pokemon-detail.component.html:

<tr><td class="pokemon-td" [class.cool-bool]="detail.isCool">{{ detail.id }} : {{ detail.name }}{{ detail.isCool == true ? "is COOL" : "is NOT COOL" }}</td>
</tr>

运行 ng serve:
在这里插入图片描述

3. Input 装饰器

3.1 Angular doc

Input 装饰器用来标记一个类字段为输入属性,并提供配置元数据。input 属性与模板中的一个DOM属性绑定。在变更检测期间,Angular会自动用DOM属性的值更新数据属性。

(Decorator that marks a class field as an input property and supplies configuration metadata. The input property is bound to a DOM property in the template. During change detection, Angular automatically updates the data property with the DOM property’s value.)

3.2 stack overflow

pokemon-detail 是一个子组件,它被设计用于插入到一个拥有 detail 数据的父组件中。此 detail 数据通过被 @Input 装饰器标记为 input 的 detail实例变量传递到 pokemon-detail 组件中。

用法:使用 input 装饰器标记实例变量,使父组件可以通过此变量传数据下来。

@Input()detail: Pokemon; 

父组件接下来将会使用子组件,并传数据给它。

<pokemon-detail [detail]="pokemon"></pokemon-detail>

父组件名为 pokemon 的实例变量含有数据,这些数据将传递到 pokemon-detail 组件中。


Angular For Beginners

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

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

蓝桥杯c/c++程序设计——数位排序

数位排序【第十三届】【省赛】【C组】 题目描述 小蓝对一个数的数位之和很感兴趣&#xff0c;今天他要按照数位之和给数排序。 当两个数各个数位之和不同时&#xff0c;将数位和较小的排在前面&#xff0c;当数位之和相等时&#xff0c;将数值小的排在前面。 例如&#xff0…

whistle网络监控 fiddler的开源替代

github源码&#xff1a;https://github.com/avwo/whistle 官网说明&#xff1a;http://wproxy.org/whistle/ windows/mac一键安装 先安装nodejs 然后运行命令 npm i -g whistle && w2 start --init启动 w2 start停止 w2 stop注意停止后要手动关闭代理服务器设置 w…

一个利用摸鱼时间背单词的软件

大家好&#xff0c;我是 Java陈序员。 最近进入了考试季&#xff0c;各种考试&#xff0c;英语四六级、考研、期末考等。不知道大家的英语四六级成绩怎么样呢&#xff1f; 记得大学时&#xff0c;英语四级都是靠高中学习积累的老本才勉强过关。 而六级则是考了多次&#xff…

Yolov5水果分类识别+pyqt交互式界面

Yolov5 Fruits Detector Yolov5 是一种先进的目标检测算法&#xff0c;可以应用于水果分类识别任务。结合 PyQT 框架&#xff0c;可以创建一个交互式界面&#xff0c;使用户能够方便地上传图片并获取水果分类结果。以下将详细阐述 Yolov5 水果分类识别和 PyQT 交互式界面的实现…

助欣科技携手鼎捷雅典娜,重磅打造“车间智报工”云端新应用

在专注于制造业整体信息化解决方案并积极进行软件开发的同时&#xff0c;助欣科技也思考着一项重要议题&#xff1a;如何在数智化转型大视野中聚焦行业痛点&#xff1f;关于这个问题&#xff0c;助欣科技给出了自己的答案——车间智报工。 行业放大镜&#xff1a;聚焦中小型制造…

阿里云服务器本地localhost换成本地的IP地址后不能访问的原因

阿里云服务器本地localhost换成本地的IP地址后不能访问的原因 问题在安装以及配置都没有问题的情况下我使用localhost:5001&#xff08;127.0.0.1:5001&#xff09;都可以正常使用&#xff0c;但是我是用本机Ip地址的时候发现无法打开网页以上这个问题出现在我阿里云的服务器上…

Java_Stream流

一、JDK8新特性&#xff08;Stream流&#xff09; 接下来学习一个全新的知识&#xff0c;叫做Stream流&#xff08;也叫Stream API&#xff09;。它是从JDK8以后才有的一个新特性&#xff0c;是专业用于对集合或者数组进行便捷操作的。有多方便呢&#xff1f;我们用一个案例体…

vue3组件通信(父给子传参,子调用父的方法,父调用子的方法,顶层组件给底层组件传参,底层组件调用顶层组件的方法)

目录 1.父传子&#xff08;父给子传参&#xff09; 2.子传父&#xff08;子调用父的方法&#xff09; 3.父调用子的方法 4.顶层给底层传参&#xff0c;底层调用顶层的方法 5.模板引用 1.父传子&#xff08;父给子传参&#xff09; ①.步骤 父组件中给子组件通过绑定属性…

48V转12V 300mA降压芯片,60V耐压、0.6A稳压芯片带ECO模式-AH590L

AH590L是一种48V转12V 300mA降压芯片&#xff0c;具有60V耐压、0.6A稳压电流的特点&#xff0c;并且还带有ECO模式&#xff0c;是一种理想的开关电源解决方案。 AH590L是PWM模式 DC/DC降压转换器。TEL&#xff1a;l86*4884*3702*宽输入电压范围4至60V适用于工业领域的广泛应用…

Java版企业电子招投标系统源代码,支持二次开发,采用Spring cloud微服务架构

在数字化时代&#xff0c;企业需要借助先进的数字化技术来提高工程管理效率和质量。招投标管理系统作为企业内部业务项目管理的重要应用平台&#xff0c;涵盖了门户管理、立项管理、采购项目管理、采购公告管理、考核管理、报表管理、评审管理、企业管理、采购管理和系统管理等…

服务器扩容未生效、不成功:解决方法

记一次解决服务器扩容未生效的解决办法 老板&#xff1a;失忆啊&#xff0c;我花钱给服务器扩容了10000000G&#xff0c;但是数据库和mq都还是用不了&#xff0c;到底是不是服务器磁盘满了&#xff0c;你到底有没有查一下什么原因导致服务用不了啊。 失忆&#xff1a;老板您确…

利用vb开发图片加密软件怎么样?

随着科技的发展&#xff0c;图片加密软件已经成为了我们生活中不可或缺的一部分。它不仅可以保护我们的隐私&#xff0c;还可以防止我们的图片被不法分子盗用。那么&#xff0c;如果我们利用VB(Visual Basic)来开发这样的软件会怎样呢&#xff1f;本文将从技术可行性、开发难度…