目标:实现如图功能(可扩展成年选择器,年-月选择器,年月日选择器)
第一步:先创建shared文件夹,该文件夹可以放一些常用的组件:
第二步:编写控件(date):
有两个需要注意的地方:
- @ViewChild,使用这个的时候,angular 8 Expected 2 arguments,需要有两个参数;
- @Input() type,这个是用来接收时间类型(‘yearPicker’,‘monthPicker’,‘dayPicker’);
第三步:在需要的地方引用该控件:
<app-date style="width:166px;height: 30px;display: flex;flex-direction: row;"
[type]="timeType"
(emitDate)="changeDate($event)">
</app-date>
拿到控件里值:
changeDate(ev){ //receive data from date component
console.log(ev);
}
完整代码:date组件完整代码 ;
不知道如何管理公共组件的,可以查看这里:Angular管理共享组件