获取对应天数的方法简单封装--更新

获取对应天数简单的封装了一下

没封装前:


 封装后:


封装代码:

新建一个名字为utility的js文件
this.ruleForm.weekValue && this.ruleForm.weekValue1这里没有封装下次用起成一样的名字就可以了

//获取开始日期对应星期几,这时就会用到data中定义的空字符串接收
export function getWeekStart(
    date,
    dayOne,
    dayTwo,
    dayThree,
    dayFour,
    dayFive,
    daySix,
    daySeven
) {
    if (date.getDay() == 0) this.ruleForm.weekValue = dayOne;
    if (date.getDay() == 1) this.ruleForm.weekValue = dayTwo;
    if (date.getDay() == 2) this.ruleForm.weekValue = dayThree;
    if (date.getDay() == 3) this.ruleForm.weekValue = dayFour;
    if (date.getDay() == 4) this.ruleForm.weekValue = dayFive;
    if (date.getDay() == 5) this.ruleForm.weekValue = daySix;
    if (date.getDay() == 6) this.ruleForm.weekValue = daySeven;
}


//获取结束日期对应星期几,这时就会用到data中定义的空字符串接收
export function getWeekEnd(
    date,
    dayOne,
    dayTwo,
    dayThree,
    dayFour,
    dayFive,
    daySix,
    daySeven
) {
    if (date.getDay() == 0) this.ruleForm.weekValue1 = dayOne;
    if (date.getDay() == 1) this.ruleForm.weekValue1 = dayTwo;
    if (date.getDay() == 2) this.ruleForm.weekValue1 = dayThree;
    if (date.getDay() == 3) this.ruleForm.weekValue1 = dayFour;
    if (date.getDay() == 4) this.ruleForm.weekValue1 = dayFive;
    if (date.getDay() == 5) this.ruleForm.weekValue1 = daySix;
    if (date.getDay() == 6) this.ruleForm.weekValue1 = daySeven;
}
export default {
    getWeekStart,
    getWeekEnd
};
main.js中引入刚刚的js文件

并给原型添加新属性后使用

需要解构赋值

import { getWeekStart } from "./common/utility/utility.js";
import { getWeekEnd } from "./common/utility/utility.js";
Vue.prototype.$getWeekStart = getWeekStart;
Vue.prototype.$getWeekEnd = getWeekEnd;
对应组件中使用
事件中:
changeDate(val) {
      this.$getWeekStart(val[0], "0", "1", "2", "3", "4", "5", "6");
      this.$getWeekEnd(val[1], "0", "1", "2", "3", "4", "5", "6");
      console.log("获取周-", this.ruleForm.weekValue, this.ruleForm.weekValue1);
    }
原文地址:https://www.cnblogs.com/home-/p/11794153.html