欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Angular7中创建组件/自定义指令/管道的方法实例详解

程序员文章站 2022-06-17 17:03:03
组件 使用命令创建组件 •创建组件的命令:ng generate component 组件名 •生成的组件组成: 组件名.html 、组件...

组件

使用命令创建组件

•创建组件的命令:ng generate component 组件名
•生成的组件组成: 组件名.html 、组件名.ts、组件名.less、组件名.spec.ts
•在组件的控制器

@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: [....]
})

指令

认识指令

•说明:在 angular 中有三种类型的指令: ◦1.组件 — 拥有模板的指令
◦2.结构型指令 — 通过添加和移除 dom 元素改变 dom 布局的指令
◦3.属性型指令 — 改变元素、组件或其它指令的外观和行为的指令。

自定义指令

•创建自定义指令的命令: ng g d 目录/指令名称

•创建指令

1.创建指令的文件ts文件

2.在指令文件中写

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

•说明:async 管道会订阅一个 observable 或 promise,并返回它发出的最近一个值。 当新值到来时,async 管道就会把该组件标记为需要进行变更检测。当组件被销毁时,async 管道就会自动取消订阅,以消除潜在的内存泄露问题。

currencypipe

•说明:把数字转换成金额字符串, 根据本地化规则进行格式化,这些规则会决定分组大小和分组分隔符、小数点字符以及其它与本地化环境有关的配置项。

datepipe

•说明:把数字转换成金额字符串, 根据本地化规则进行格式化,这些规则会决定分组大小和分组分隔符、小数点字符以及其它与本地化环境有关的配置项。

decimalpipe

•说明:把数字转换成字符串, 根据本地化规则进行格式化,这些规则会决定分组大小和分组分隔符、小数点字符以及其它与本地化环境有关的配置项。

自定义管道

•创建管道的命令:ng g pipe 目录/管道名称

•手动创建管道 •创建ts文件

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' ? "男" : "女"
 }
}

•将这个管道添加到ngmoduel中

// 1. 先导入
import { genderpipe } from "./diner.gender";

// 2.导出 
export const diner_components: provider[] = [genderpipe];

// 3.添加到ngmodule中的
@ngmodule({
 // 这里列出的 ngmodule 所导出的可声明对象可用在当前模块内的模板中
 imports: [...],

 // declarations:[ 组件 ] 属于该模块的一组组件、指令和管道(统称可声明对象)。
 // 注意点:在这个源数据中只能声明组件、管道、指令
 declarations: [diner_components],
 // 定义此 ngmodule 中要编译的组件集,这样它们才可以动态加载到视图中。
 entrycomponents: [...]
})

•在页面中引入使用

~
<!-- item.dgender的值为m和w,将对应的m转为男,w转为女 -->
<td>{{item.dgender | getgender}}</td>
~

总结

以上所述是小编给大家介绍的angular7中创建组件/自定义指令/管道的方法实例详解,希望对大家有所帮助