国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

Angular7中創建組件/自定義指令/管道的方法實例詳解

2024-05-06 15:41:11
字體:
來源:轉載
供稿:網友

組件

使用命令創建組件

•創建組件的命令: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: []})            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 蒙阴县| 五河县| 瑞安市| 黎平县| 台州市| 巴东县| 建德市| 邵东县| 鸡西市| 称多县| 大余县| 吴旗县| 通榆县| 柘荣县| 古蔺县| 巴彦淖尔市| 杨浦区| 崇仁县| 靖宇县| 兴山县| 柘城县| 乐业县| 峨眉山市| 昌江| 富川| 界首市| 呼和浩特市| 安远县| 寿阳县| 正宁县| 庆云县| 云南省| 铁岭县| 乐昌市| 壶关县| 东宁县| 民勤县| 金阳县| 梅州市| 宁化县| 南涧|