组件
使用命令创建组件
"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中创建组件/自定义指令/管道的方法实例详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。