组件
使用命令创建组件
"htmlcode">
@Component({
selector: 'app-heroes',
templateUrl: './heroes.component.html',
styleUrls: ['./heroes.component.less']
})
手动创建组件
1.创建一个组件ts文件
2.在组件中设置
// 1. 导入包,按需导入
import { Component } from "@angular/core";
import { CoreEdit, NavLayoutComponent } from "@reco/core";
import { DinerService } from "../Service";
// 2.定义当前组件的修饰器
@Component({
// 支出对外使用的名称
selector: "diner-birth",
// 使用的模板
templateUrl: "./diner.birth.html"
})
// 导出使用的类
export class DinerBirthComponent extends CoreEdit {
constructor(
private _dinerService: DinerService,
layout: NavLayoutComponent
) {
super(_dinerService, 'diner-birth', layout);
}
}
1.在index.ts文件中引入并导出
// 1. 导入
import { DinerBirthComponent } from "./diner.birth";
// 2. 导出
export { DinerBirthComponent }
// 3. 注册
@NgModule({
// 这里列出的 NgModule 所导出的可声明对象可用在当前模块内的模板中
imports: [....],
// declarations:[ 组件 ] 属于该模块的一组组件、指令和管道(统称可声明对象)。
// 注意点:在这个源数据中只能声明组件、管道、指令
declarations: [DinerBirthComponent],
// 定义此 NgModule 中要编译的组件集,这样它们才可以动态加载到视图中。
entryComponents: [....],
// 导出的模块
exports: [....]
})
指令
认识指令
"htmlcode">
import { Directive, ElementRef, Input, Output } from '@angular/core';
// 自定义指令
@Directive({
selector: '[dinerHidden]'
})
// 导出指令的模块
export class DinerHiddenDirective {
// el 代表当前的元素
constructor(el: ElementRef) {
// console.log()
el.nativeElement.style.display = "none"
}
}
1.在index.ts中将该指令导入到ngModule中
// 1.导入
import { DinerHiddenDirective } from "./diner.hidden";
// 2.导出
export const DINER_COMPONENTS: Provider[] = [ DinerHiddenDirective ];
// 3.ngModule中注册
@NgModule({
// 这里列出的 NgModule 所导出的可声明对象可用在当前模块内的模板中
imports: [],
// declarations:[ 组件 ] 属于该模块的一组组件、指令和管道(统称可声明对象)。
// 注意点:在这个源数据中只能声明组件、管道、指令
declarations: [DINER_COMPONENTS],
// 定义此 NgModule 中要编译的组件集,这样它们才可以动态加载到视图中。
entryComponents: []
})
1.在页面中引用
<!-- 隐藏当前的这个标签 --> <div class="form-group col-sm-6" dinerHidden> </div>
管道中的常用API
asyncPipe
"color: #ff0000">自定义管道
"htmlcode">
import { Pipe, PipeTransform } from '@angular/core';
// 自定义管道 getGender
@Pipe({
name: 'getGender'
})
// 创建的管道的类
export class GenderPipe implements PipeTransform {
transform(value: string, exponent: string) {
if (value == ' ') return "未知"
return value === 'm' "男" : "女"
}
}
"htmlcode">
// 1. 先导入
import { GenderPipe } from "./diner.gender";
// 2.导出
export const DINER_COMPONENTS: Provider[] = [GenderPipe];
// 3.添加到NgModule中的
@NgModule({
// 这里列出的 NgModule 所导出的可声明对象可用在当前模块内的模板中
imports: [...],
// declarations:[ 组件 ] 属于该模块的一组组件、指令和管道(统称可声明对象)。
// 注意点:在这个源数据中只能声明组件、管道、指令
declarations: [DINER_COMPONENTS],
// 定义此 NgModule 中要编译的组件集,这样它们才可以动态加载到视图中。
entryComponents: [...]
})
"htmlcode">
~
<!-- item.DGender的值为m和w,将对应的m转为男,w转为女 -->
<td>{{item.DGender | getGender}}</td>
~
总结
以上所述是小编给大家介绍的Angular7中创建组件/自定义指令/管道的方法实例详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
暂无“Angular7中创建组件/自定义指令/管道的方法实例详解”评论...
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。
更新动态
2025年11月06日
2025年11月06日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]