39-Vue之cron表达式组件使用


前言

  • 本篇来学习下vue中如何生成cron表达式的两个包

vue-cron-editor-buefy

1. 安装vue-cron-editor-buefy包
npm install vue-cron-editor-buefy -S
npm install cronstrue -S
2. 使用
  • 在src/views下 新建cronTest.vue.vue文件,内容如下
<template>
    <div id="app">
      <!-- 输入框   -->
      <el-form :model="form" label-position="left">     
        <el-form-item label="cron表达式" :label-width="formLabelWidth">
          <el-input v-model="form.cron_rule" autocomplete="off" placeholder="请输入定时cron表达式" disabled>
            <template slot="append">
              <el-button type="primary" @click="creatCron">
                生成表达式
                <i class="el-icon-time el-icon--right"></i>
              </el-button>
            </template>
          </el-input>
        </el-form-item>

        <!-- cron dialog -->
        <el-dialog title="生成 cron" :visible.sync="showCron">
            <VueCronEditorBuefy v-model="expression"/>
            <div style="color:#2496FF">
                <h5>预览cron表达式: {{ expression }}</h5>
                <h5>cron表达式解释: {{ explainAddCron }}</h5>
            </div>
            <el-button type="primary" size="medium" @click="cronConfirm" style="margin-left: 88%;">确定
            </el-button>
        </el-dialog>
       
      </el-form>
    </div>
  </template>
  <script>
  import VueCronEditorBuefy from 'vue-cron-editor-buefy';
  import cronstrue from "cronstrue/i18n";
  export default {
      components: { cronstrue,VueCronEditorBuefy, },
      data() {
          return {
              input: "",
              expression: "",
              showCron: false,
              cronExpression: "*/1 * * * *",
              form:{},
              formLabelWidth: '120px',
              showCron: false
          };
      },
      computed: {
       explainAddCron() {
        if(this.expression){
        // use24HourTimeFormat : 使用24小时制, 默认false 12小时制
          return cronstrue.toString(this.expression, { locale: "zh_CN",use24HourTimeFormat:true});
        }
        
      }
    },
      methods: {
          creatCron(){
            // 生成表达式
            this.expression = this.cronExpression 
            this.showCron = true
          },        
        cronConfirm() {
            // 生成表达式确定按钮
            this.showCron = false
            this.form.cron_rule = this.expression

            },
          
      }
  };
  </script>  
3. 配置路由
  • 在router.js中添加如下内容
{
    path: '/cron',
    component: Layout,
    children: [
      {
        path: 'cron',
        name: 'cron',
        component: () => import('@/views/cronTest.vue'),
        meta: { title: 'cron', icon: 'form' }
      }
    ]
  }

在这里插入图片描述

4. 运行并查看效果

在这里插入图片描述

vcrontab

1. 安装vcrontab包
npm install  vcrontab -S
2. 使用
  • 在src/views下 新建cronTest.vue.vue文件,内容如下
<template>
    <div id="app">
      <div class="box">
          <el-input v-model="input" placeholder class="inp"></el-input>
          <el-button type="primary" @click="showDialog">生成 cron</el-button>
      </div>
      <el-dialog title="生成 cron" :visible.sync="showCron">
          <vcrontab @hide="showCron=false" @fill="crontabFill" :expression="expression"></vcrontab>
      </el-dialog>
    </div>
  </template>
  <script>
  import vcrontab from 'vcrontab'

  export default {
      components: {vcrontab},
      data() {
          return {
              input: "",
              expression: "",
              showCron: false,
              cronExpression: "*/1 * * * *",
              form:{},
              formLabelWidth: '120px',
              showCron: false
          };
      },
      methods: {
          crontabFill(value) {
              //确定后回传的值
              this.input = value;
          },
          showDialog() {
              this.expression = this.input;//传入的 cron 表达式,可以反解析到 UI 上
              this.showCron = true;
          }
          
      }
  };
  </script>
  
3. 配置路由
  • 同上
4. 运行并查看

在这里插入图片描述

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱学习de测试小白

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值