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

简介: 38-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. 运行并查看

相关文章
|
2月前
|
JavaScript
Vue中如何实现兄弟组件之间的通信
在Vue中,兄弟组件可通过父组件中转、事件总线、Vuex/Pinia或provide/inject实现通信。小型项目推荐父组件中转或事件总线,大型项目建议使用Pinia等状态管理工具,确保数据流清晰可控,避免内存泄漏。
300 2
|
1月前
|
缓存 JavaScript
vue中的keep-alive问题(2)
vue中的keep-alive问题(2)
280 137
|
5月前
|
人工智能 JavaScript 算法
Vue 中 key 属性的深入解析:改变 key 导致组件销毁与重建
Vue 中 key 属性的深入解析:改变 key 导致组件销毁与重建
769 0
|
4月前
|
人工智能 JSON JavaScript
VTJ.PRO 首发 MasterGo 设计智能识别引擎,秒级生成 Vue 代码
VTJ.PRO发布「AI MasterGo设计稿识别引擎」,成为全球首个支持解析MasterGo原生JSON文件并自动生成Vue组件的AI工具。通过双引擎架构,实现设计到代码全流程自动化,效率提升300%,助力企业降本增效,引领“设计即生产”新时代。
403 1
|
4月前
|
JavaScript 安全
在 Vue 中,如何在回调函数中正确使用 this?
在 Vue 中,如何在回调函数中正确使用 this?
259 0
|
JavaScript 测试技术 容器
Vue2+VueRouter2+webpack 构建项目
1). 安装Node环境和npm包管理工具 检测版本 node -v npm -v 图1.png 2). 安装vue-cli(vue脚手架) npm install -g vue-cli --registry=https://registry.
1229 0
|
7月前
|
JavaScript
vue实现任务周期cron表达式选择组件
vue实现任务周期cron表达式选择组件
1006 4
|
5月前
|
JavaScript UED
用组件懒加载优化Vue应用性能
用组件懒加载优化Vue应用性能
|
6月前
|
JavaScript 数据可视化 前端开发
基于 Vue 与 D3 的可拖拽拓扑图技术方案及应用案例解析
本文介绍了基于Vue和D3实现可拖拽拓扑图的技术方案与应用实例。通过Vue构建用户界面和交互逻辑,结合D3强大的数据可视化能力,实现了力导向布局、节点拖拽、交互事件等功能。文章详细讲解了数据模型设计、拖拽功能实现、组件封装及高级扩展(如节点类型定制、连接样式优化等),并提供了性能优化方案以应对大数据量场景。最终,展示了基础网络拓扑、实时更新拓扑等应用实例,为开发者提供了一套完整的实现思路和实践经验。
831 77
|
7月前
|
缓存 JavaScript 前端开发
Vue 基础语法介绍
Vue 基础语法介绍