vue3中定時任務(wù)cron表達(dá)式組件的比較分析
更新時間:2025年10月25日 15:14:18 作者:Qredsun
文章比較了三種適用于Vue3的cron組件:vue3-cron-plus、no-vue3-cron和vue3-cron-plus-picker,每種組件的安裝和使用實例都詳細(xì)列出,最終對比發(fā)現(xiàn),vue3-cron-plus-picker不僅可以從組件中獲取cron表達(dá)式,還能顯示任務(wù)的執(zhí)行時間,因此推薦使用

背景
之前使用vue2開發(fā)項目時,使用了cron組件,比較了兩種組件的使用效果。
現(xiàn)在需要把原有的vue2項目升級為vue3,需要對應(yīng)的cron組件。
方案一、vue3-cron-plus

具體實現(xiàn):
安裝插件
npm install vue3-cron-plus -S
vue文件使用實例:
<template>
<div>
<el-input class="elInput" v-model="cronValue" @click="openDialog" :clearable="true" placeholder="請輸入正確的cron表達(dá)式">
</el-input>
<el-dialog v-model="showCron">
<vue3CronPlus
@change="changeCron"
@close="closeDialog"
max-height="600px"
i18n="cn">
</vue3CronPlus>
</el-dialog>
</div>
</template>
<script>
import { vue3CronPlus } from 'vue3-cron-plus'
import 'vue3-cron-plus/dist/index.css' // 引入樣式
export default {
name : "DemoCompare",
components: { "vue3CronPlus":vue3CronPlus },
data () {
return{
cronValue:"",
showCron:"",
}
},
methods : {
openDialog () {
this.showCron = true;
},
closeDialog(){
this.showCron = false;
},
changeCron(cronValue){
if (typeof (cronValue) == "string") {
this.cronValue = cronValue;
}
}
}
}
</script>
<style scoped>
</style>
方案二、no-vue3-cron

具體實現(xiàn):
安裝插件
npm install no-vue3-cron -S
vue文件使用實例:
<template>
<div>
<el-input class="elInput" v-model="cronValue" @click="openDialog" :clearable="true" placeholder="請輸入正確的cron表達(dá)式">
</el-input>
<el-dialog v-model="showCron">
<noVue3Cron
:cron-value="cronValue"
@change="changeCron"
@close="closeDialog"
max-height="600px"
i18n="cn">
</noVue3Cron>
</el-dialog>
</div>
</template>
<script>
//局部引入
import { noVue3Cron } from 'no-vue3-cron'
import 'no-vue3-cron/lib/noVue3Cron.css' // 引入樣式
export default {
name : "DemoCompareShow",
components: { "noVue3Cron":noVue3Cron },
data () {
return{
cronValue:"",
showCron:"",
expression:"* * * * * * *"
}
},
methods : {
openDialog () {
this.showCron = true;
if (this.cronValue != "") {
this.expression = this.cronValue
}
},
closeDialog(){
this.showCron = false;
},
changeCron(cronValue){
if (typeof (cronValue) == "string") {
this.cronValue = cronValue;
}
}
}
}
</script>
<style scoped>
</style>
方案三、vue3-cron-plus-picker

具體實現(xiàn):
安裝插件
npm install vue3-cron-plus-picker -S
vue文件使用實例:
<template>
<div>
<el-input class="elInput" v-model="cronValue" @click="openDialog" :clearable="true" placeholder="請輸入正確的cron表達(dá)式">
</el-input>
<el-dialog v-model="showCron">
<Vue3CronPlusPicker @hide="closeDialog" @fill="fillValue" :expression="expression"/>
</el-dialog>
</div>
</template>
<script >
import 'vue3-cron-plus-picker/style.css'
import {Vue3CronPlusPicker} from 'vue3-cron-plus-picker'
export default {
name : "demoShow",
components : {"Vue3CronPlusPicker":Vue3CronPlusPicker,},
data () {
return{
cronValue:"",
showCron:"",
expression:"* * * * * * *"
}
},
methods : {
openDialog () {
this.showCron = true;
if (this.cronValue != ""){
this.expression = this.cronValue
}
},
closeDialog(){
this.showCron = false;
},
fillValue(cronValue){
this.cronValue = cronValue;
}
}
}
</script>
<style lang="scss" scoped>
</style>
對比
- 都可以從組件中獲取cron的表達(dá)式
vue3-cron-plus組件不能根據(jù)cron表達(dá)式回顯到組件vue3-cron-plus-picker組件可以看到將來執(zhí)行任務(wù)的具體時間,推薦使用
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
如何使用fetchEventSource實現(xiàn)sse流式請求
這篇文章主要介紹了如何使用fetchEventSource實現(xiàn)sse流式請求問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08
vue項目中使用eslint+prettier規(guī)范與檢查代碼的方法
這篇文章主要介紹了vue項目中使用eslint+prettier規(guī)范與檢查代碼的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01
非Vuex實現(xiàn)的登錄狀態(tài)判斷封裝實例代碼
這篇文章主要給大家介紹了關(guān)于非Vuex實現(xiàn)的登錄狀態(tài)判斷封裝的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2022-02-02
使用vuex解決刷新頁面state數(shù)據(jù)消失的問題記錄
這篇文章主要介紹了使用vuex解決刷新頁面state數(shù)據(jù)消失的問題記錄,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05

