DayJs源码(五)Dayjs插件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DayJs源码(五)Dayjs插件相关的知识,希望对你有一定的参考价值。
参考技术A
在DayJS中存在许多方法需要结合插件使用的例子。在DayJS中是怎样实现插件功能呢?
首先,dayJS注册插件方法在index.js
首先extend方法支持传递两个参数,分别是插件本身和插件参数。函数里面将调用插件,同时传入三个参数,分别是插件参数,Dayjs类和dayjs函数。
咱们直接找一个插件看看里面的实现方法。存放插件的贷方就在plugin文件夹中
以dayOfyear插件为例,此方法返回一个number,表示Dayjs的年份,或者设置年份的日期。源码如下:
typescript 带有dayjs和rxjs的简单角度计数器/倒计时组件
/**
*
* Example Usage:
*
* # Counter
* const now = dayjs()
* <app-counter [autostart]="true" [mode]="forwards" [startTime]="now"></app-counter>
*
* # Countdown
* const later = dayjs().add(1, 'minute')
* <app-counter [autostart]="true" [mode]="backwards" [stopTime]="later"></app-counter>
*
*/
import { Component, Input, OnDestroy, OnInit } from '@angular/core';
import * as dayjs from 'dayjs';
import { interval, Subject, Subscription } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
@Component({
selector: 'app-counter',
template: `{{ timeFormatted }}`,
})
export class CounterComponent implements OnInit, OnDestroy {
// Forwards-Mode (Counter)
@Input() forwards: boolean
@Input() startTime: Date
public elapsedSeconds: number
public counterHasStarted: boolean
// Backwards-Mode (Countdown)
@Input() backwards: boolean
@Input() stopTime: Date
public countdownHasFinished: boolean
public remainingSeconds: number
public timeFormatted: string
@Input() autostart: boolean
@Input() updateInterval: number = 1000 / 2
public timeInterval$: Subscription
private unsubscribe$ = new Subject()
constructor() { }
ngOnInit() {
if (this.autostart) {
this.start()
}
}
ngOnDestroy() {
this.stop()
}
/**
* Initializes the Counter / Countdown
*/
public start() {
if (this.forwards == this.backwards) {
console.error("Couldn't start counter as no mode or both modes are set.")
return
}
if (this.forwards && (!this.startTime || !dayjs(this.startTime).isValid())) {
console.error("Couldn't start counter as mode is 'forwards' but no start-time is provided.")
return
}
if (this.backwards && (!this.stopTime || !dayjs(this.stopTime).isValid())) {
console.error("Couldn't start counter as mode is 'forwards' but no start-time is provided.")
return
}
// Start Interval
this.timeInterval$ = interval(this.updateInterval).startWith(0).pipe(
takeUntil(this.unsubscribe$)
).subscribe(_ => {
this.updateTime()
})
}
/**
* Stops the Counter / Countdown
*/
public stop() {
this.unsubscribe$.next(true)
this.unsubscribe$.complete()
}
/**
* Updates `timeFormatted` of the Counter / Countdown
*/
private updateTime() {
const now = dayjs()
if (this.forwards) {
// Start-Time from which the counter gets increased
const startTime = dayjs(this.startTime)
this.counterHasStarted = now.isAfter(startTime)
if (!this.counterHasStarted) {
this.timeFormatted = '0:00'
this.elapsedSeconds = 0
return
}
let elapsedTime = dayjs(now.valueOf() - startTime.valueOf())
elapsedTime = elapsedTime.subtract(dayjs().utcOffset(), 'minute')
this.elapsedSeconds = now.diff(startTime, 'second')
const format = elapsedTime.hour() ? 'H:mm:ss' : 'm:ss'
this.timeFormatted = elapsedTime.format(format)
} else if (this.backwards) {
// Stop-Time until which the countdown gets decreased
const stopTime = dayjs(this.stopTime)
this.countdownHasFinished = now.isAfter(stopTime)
if (this.countdownHasFinished) {
this.timeFormatted = '0:00'
this.remainingSeconds = 0
return
}
let remainingTime = dayjs(stopTime.valueOf() - now.valueOf())
remainingTime = remainingTime.subtract(dayjs().utcOffset(), 'minute')
this.remainingSeconds = stopTime.diff(now, 'second')
const format = remainingTime.hour() ? 'H:mm:ss' : 'm:ss'
this.timeFormatted = remainingTime.format(format)
}
}
}
以上是关于DayJs源码(五)Dayjs插件的主要内容,如果未能解决你的问题,请参考以下文章
vue项目使用dayjs
[moment][dayjs]使用的小小区别
dayjs时间处理库的基本使用
moment.js与dayjs安装与使用
轻量的处理时间和日期的Day.js库-使用案例
Dayjs转换时间