如何在Element-UI中设置DateTimePicker时间范围选择器的默认时间?

发布于 2025-02-12 01:02:49 字数 1054 浏览 0 评论 0原文

我要设置的默认时间范围是00:00:00 23:59:59当天。 我可以使用v-Modeldata中绑定value1,但是value2绑定到的滚动计算会报告错误,为什么? 完整的代码如下:

new Vue({
    el: '#app',
    data: function () {
      return {
        value1: [new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDay(), 0, 0, 0),
          new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDay(), 23, 59, 59)
        ],
      }
    },
    computed: {
      value2: {
        get() {
          let date = new Date()
          let y = date.getFullYear()
          let m = date.getMonth() + 1
          let d = date.getDay()
          let start = new Date(y, m, d, 0, 0, 0)
          let end = new Date(y, m, d, 23, 59, 59)
          return [start, end]
        },
        set(newVal) {
          this.value2 = newVal
        }
      },
    },
    methods: {
      change(e) {
        console.log('eee', e);
      }
    }
  })

The default time range I want to set is 00:00:00 to 23:59:59 for the current day.
I can use v-model to bind value1 in data, but the scroll of value2 bound to computed will report an error, why is this?
The complete code is as follows:

new Vue({
    el: '#app',
    data: function () {
      return {
        value1: [new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDay(), 0, 0, 0),
          new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDay(), 23, 59, 59)
        ],
      }
    },
    computed: {
      value2: {
        get() {
          let date = new Date()
          let y = date.getFullYear()
          let m = date.getMonth() + 1
          let d = date.getDay()
          let start = new Date(y, m, d, 0, 0, 0)
          let end = new Date(y, m, d, 23, 59, 59)
          return [start, end]
        },
        set(newVal) {
          this.value2 = newVal
        }
      },
    },
    methods: {
      change(e) {
        console.log('eee', e);
      }
    }
  })

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

阪姬 2025-02-19 01:02:49

您不能直接set计算中的变量本身,这将导致无限循环。通常,设置了一个临时变量,并且getset在此变量上操作。

new Vue({
  el: "#app",
  data: function () {
    return {
      value1: [
        new Date(
          new Date().getFullYear(),
          new Date().getMonth(),
          new Date().getDay(),
          0,
          0,
          0
        ),
        new Date(
          new Date().getFullYear(),
          new Date().getMonth(),
          new Date().getDay(),
          23,
          59,
          59
        ),
      ],
      value2Tmp: [],
    };
  },
  mounted() {
    let date = new Date();
    let y = date.getFullYear();
    let m = date.getMonth() + 1;
    let d = date.getDay();
    let start = new Date(y, m, d, 0, 0, 0);
    let end = new Date(y, m, d, 23, 59, 59);
    this.value2Tmp = [start, end];
  },
  computed: {
    value2: {
      get() {
        return this.value2Tmp;
      },
      set(newVal) {
        this.value2Tmp = newVal;
      },
    },
  },
  methods: {
    change(e) {
      console.log("eee", e);
    },
  },
});

You cannot directly set the variable itself in computed, which will cause an infinite loop. Generally, a temporary variable is set, and both get and set operate on this variable.

new Vue({
  el: "#app",
  data: function () {
    return {
      value1: [
        new Date(
          new Date().getFullYear(),
          new Date().getMonth(),
          new Date().getDay(),
          0,
          0,
          0
        ),
        new Date(
          new Date().getFullYear(),
          new Date().getMonth(),
          new Date().getDay(),
          23,
          59,
          59
        ),
      ],
      value2Tmp: [],
    };
  },
  mounted() {
    let date = new Date();
    let y = date.getFullYear();
    let m = date.getMonth() + 1;
    let d = date.getDay();
    let start = new Date(y, m, d, 0, 0, 0);
    let end = new Date(y, m, d, 23, 59, 59);
    this.value2Tmp = [start, end];
  },
  computed: {
    value2: {
      get() {
        return this.value2Tmp;
      },
      set(newVal) {
        this.value2Tmp = newVal;
      },
    },
  },
  methods: {
    change(e) {
      console.log("eee", e);
    },
  },
});

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文