@Component({ selector: 'app-heroes', templateUrl: './heroes.component.html', styleUrls: ['./heroes.component.less'] })
// 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. 导入 import { DinerBirthComponent } from "./diner.birth"; // 2. 导出 export { DinerBirthComponent } // 3. 注册 @NgModule({ // 这里列出的 NgModule 所导出的可声明对象可用在当前模块内的模板中 imports: [....], // declarations:[ 组件 ] 属于该模块的一组组件、指令和管道(统称可声明对象)。 // 注意点:在这个源数据中只能声明组件、管道、指令 declarations: [DinerBirthComponent], // 定义此 NgModule 中要编译的组件集,这样它们才能够动态加载到视图中。 entryComponents: [....], // 导出的模块 exports: [....] })
建立自定义指令的命令: ng g d 目录/指令名称html
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.导入 import { DinerHiddenDirective } from "./diner.hidden"; // 2.导出 export const DINER_COMPONENTS: Provider[] = [ DinerHiddenDirective ]; // 3.ngModule中注册 @NgModule({ // 这里列出的 NgModule 所导出的可声明对象可用在当前模块内的模板中 imports: [], // declarations:[ 组件 ] 属于该模块的一组组件、指令和管道(统称可声明对象)。 // 注意点:在这个源数据中只能声明组件、管道、指令 declarations: [DINER_COMPONENTS], // 定义此 NgModule 中要编译的组件集,这样它们才能够动态加载到视图中。 entryComponents: [] })
<!-- 隐藏当前的这个标签 --> <div class="form-group col-sm-6" dinerHidden> </div>
建立管道的命令:ng g pipe 目录/管道名称api
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' ? "男" : "女" } }
// 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> ~