博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
angular组件版本管理器
阅读量:6979 次
发布时间:2019-06-27

本文共 2605 字,大约阅读时间需要 8 分钟。

准备2个版本的btnRef

export abstract class BtnRef {  version: Version;}export class BtnV1 extends BtnRef {  version = new Version('1.0.0');}export class BtnV2 extends BtnRef {  version = new Version('1.0.1');}export const btnProvide: Provider[] = [{  provide: BtnRef,  useClass: BtnV1,  multi: true}, {  provide: BtnRef,  useClass: BtnV1,  multi: true}, {  provide: BtnRef,  useClass: BtnV2,  multi: true}];复制代码

-- 获取最新版本ref

export class BtnVersion {  refArray: BtnRef[] = [];  constructor(refs: BtnRef[]) {    // 去除版本号一样的ref    this.refArray = _.sortedUniqBy(_.flattenDeep(refs), (o) => o.version.full);  }  getLatest(): BtnRef {    return _.maxBy(this.refArray, (o) => o.version.full);  }}export const NEWBTNREF = new InjectionToken
('BTNREF');export function btnVersionFactory(btnRef: BtnRef) { return new BtnVersion([btnRef]);}export const BTNREF_FACTORY: Provider[] = [ { provide: BtnVersion, useFactory: btnVersionFactory, deps: [BtnRef] }, { provide: NEWBTNREF, useFactory: (version: BtnVersion) => { return version.getLatest(); }, deps: [BtnVersion] }];复制代码
  • 组件中使用
@Component({  selector: 'app-btn',  templateUrl: './btn.component.html',  styleUrls: ['./btn.component.css']})export class BtnComponent implements OnInit {  constructor(    @Inject(NEWBTNREF) public btns: BtnRef  ) { }  ngOnInit() {    console.log(this.btns);  }}复制代码
  • 去掉@Inject(NEWBTNREF)
export class BtnVersion {  refArray: BtnRef[] = [];  constructor(refs: BtnRef[]) {    // 去除版本号一样的ref    this.refArray = _.sortedUniqBy(_.flattenDeep(refs), (o) => o.version.full);  }  getLatest(): NewBtnRef {    return _.maxBy(this.refArray, (o) => o.version.full);  }}export abstract class NewBtnRef extends BtnRef { }export function btnVersionFactory(btnRef: BtnRef) {  return new BtnVersion([btnRef]);}export const BTNREF_FACTORY: Provider[] = [  {    provide: BtnVersion,    useFactory: btnVersionFactory,    deps: [BtnRef]  },  {    provide: NewBtnRef,    useFactory: (version: BtnVersion) => {      return version.getLatest();    },    deps: [BtnVersion]  }];@Component({  selector: 'app-btn',  templateUrl: './btn.component.html',  styleUrls: ['./btn.component.css']})export class BtnComponent implements OnInit {  constructor(    public btns: NewBtnRef  ) { }  ngOnInit() {    console.log(this.btns);  }}复制代码
  • 隔离
@Component({  selector: 'app-btn',  templateUrl: './btn.component.html',  styleUrls: ['./btn.component.css'],  providers: [    BTNREF_FACTORY  ]})export class BtnComponent implements OnInit {  constructor(    public btns: NewBtnRef  ) { }  ngOnInit() {    console.log(this.btns);  }}复制代码

转载地址:http://pwypl.baihongyu.com/

你可能感兴趣的文章
jQuery EasyUI 表单插件 - Datebox 日期框
查看>>
要哭了,模拟器键盘一直不显示
查看>>
获取下个月的今天
查看>>
elasticsearch简介
查看>>
文件分区格式化及挂载
查看>>
Centos运行级别和开机过程
查看>>
Linux 装B之作酷炫小工具
查看>>
Citrix Avalon安装实验手册之一----Avalon概述及实验环境准备
查看>>
动态表单构建器——建造者模式
查看>>
Android 自动化测试
查看>>
MySQL 5.5 服务器变量详解(二)
查看>>
bootstrap table
查看>>
CentOS 7 yum 安装 MySQL5.7
查看>>
企业网络翻译官——DNS
查看>>
RocketMQ3.2.2生产者发送消息自动创建Topic队列数无法超过4个
查看>>
USG防火墙telnet实验
查看>>
[给12306支招]取消车票预订-采用全额预售(充值)
查看>>
linux下使profile和.bash_profile立即生效的方法
查看>>
Operations Manager 2012 SP1配置部署系列之(二) SCOM监控SCVMM
查看>>
父域与子域之的信任关系
查看>>