Angular7中創(chuàng)建組件/自定義指令/管道的方法實例詳解
組件
使用命令創(chuàng)建組件
•創(chuàng)建組件的命令:ng generate component 組件名
•生成的組件組成: 組件名.html 、組件名.ts、組件名.less、組件名.spec.ts
•在組件的控制器
@Component({
selector: 'app-heroes',
templateUrl: './heroes.component.html',
styleUrls: ['./heroes.component.less']
})
手動創(chuàng)建組件
1.創(chuàng)建一個組件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:[ 組件 ] 屬于該模塊的一組組件、指令和管道(統(tǒng)稱可聲明對象)。
// 注意點:在這個源數(shù)據(jù)中只能聲明組件、管道、指令
declarations: [DinerBirthComponent],
// 定義此 NgModule 中要編譯的組件集,這樣它們才可以動態(tài)加載到視圖中。
entryComponents: [....],
// 導出的模塊
exports: [....]
})
指令
認識指令
•說明:在 Angular 中有三種類型的指令: ◦1.組件 — 擁有模板的指令
◦2.結構型指令 — 通過添加和移除 DOM 元素改變 DOM 布局的指令
◦3.屬性型指令 — 改變元素、組件或其它指令的外觀和行為的指令。
自定義指令
•創(chuàng)建自定義指令的命令: ng g d 目錄/指令名稱
•創(chuàng)建指令
1.創(chuàng)建指令的文件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:[ 組件 ] 屬于該模塊的一組組件、指令和管道(統(tǒng)稱可聲明對象)。
// 注意點:在這個源數(shù)據(jù)中只能聲明組件、管道、指令
declarations: [DINER_COMPONENTS],
// 定義此 NgModule 中要編譯的組件集,這樣它們才可以動態(tài)加載到視圖中。
entryComponents: []
})
1.在頁面中引用
<!-- 隱藏當前的這個標簽 --> <div class="form-group col-sm-6" dinerHidden> </div>
管道中的常用API
asyncPipe
•說明:async 管道會訂閱一個 Observable 或 Promise,并返回它發(fā)出的最近一個值。 當新值到來時,async 管道就會把該組件標記為需要進行變更檢測。當組件被銷毀時,async 管道就會自動取消訂閱,以消除潛在的內存泄露問題。
CurrencyPipe
•說明:把數(shù)字轉換成金額字符串, 根據(jù)本地化規(guī)則進行格式化,這些規(guī)則會決定分組大小和分組分隔符、小數(shù)點字符以及其它與本地化環(huán)境有關的配置項。
DatePipe
•說明:把數(shù)字轉換成金額字符串, 根據(jù)本地化規(guī)則進行格式化,這些規(guī)則會決定分組大小和分組分隔符、小數(shù)點字符以及其它與本地化環(huán)境有關的配置項。
DecimalPipe
•說明:把數(shù)字轉換成字符串, 根據(jù)本地化規(guī)則進行格式化,這些規(guī)則會決定分組大小和分組分隔符、小數(shù)點字符以及其它與本地化環(huán)境有關的配置項。
自定義管道
•創(chuàng)建管道的命令:ng g pipe 目錄/管道名稱
•手動創(chuàng)建管道 •創(chuàng)建ts文件
import { Pipe, PipeTransform } from '@angular/core';
// 自定義管道 getGender
@Pipe({
name: 'getGender'
})
// 創(chuàng)建的管道的類
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:[ 組件 ] 屬于該模塊的一組組件、指令和管道(統(tǒng)稱可聲明對象)。
// 注意點:在這個源數(shù)據(jù)中只能聲明組件、管道、指令
declarations: [DINER_COMPONENTS],
// 定義此 NgModule 中要編譯的組件集,這樣它們才可以動態(tài)加載到視圖中。
entryComponents: [...]
})
•在頁面中引入使用
~
<!-- item.DGender的值為m和w,將對應的m轉為男,w轉為女 -->
<td>{{item.DGender | getGender}}</td>
~
總結
以上所述是小編給大家介紹的Angular7中創(chuàng)建組件/自定義指令/管道的方法實例詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關文章
angularjs過濾器--filter與ng-repeat配合有奇效
本篇文章主要介紹了angularjs過濾器-filter與ng-repeat的相關知識。具有很好的參考價值。下面跟著小編一起來看下吧2017-04-04
詳解webpack+angular2開發(fā)環(huán)境搭建
這篇文章主要介紹了詳解webpack+angular2開發(fā)環(huán)境搭建,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06
使用ngView配合AngularJS應用實現(xiàn)動畫效果的方法
這篇文章主要介紹了使用ngView配合AngularJS應用實現(xiàn)動畫效果的方法,AngularJS是十分熱門的JavaScript庫,需要的朋友可以參考下2015-06-06
詳解AngularJs中$sce與$sceDelegate上下文轉義服務
這篇文章給大家詳細介紹了AngularJs提供的嚴格上下文轉義服務$sce與$sceDelegate,文中介紹的很詳細,有需要的朋友們可以參考借鑒。2016-09-09

