Home > Web Front-end > JS Tutorial > body text

The datepicker plug-in in Vue cannot monitor the value of the datepicker input box

亚连
Release: 2018-06-09 11:12:56
Original
1852 people have browsed it

This article mainly introduces the solution to the problem that Vue cannot monitor the value of the datepicker input box when referencing the third-party datepicker plug-in. It has certain reference value. Interested friends can refer to it

一, Background

The third-party datepicker plug-in is used in the Vue project. After selecting the date, Vue cannot detect the change of the datepicker input box

 <label class="fl">日期:</label>
 <p class="input-wrapper fr">
  <input class="daterangepicker" ref="datepicker" v-model="dateRange"/>
  <a href="javascript:;" rel="external nofollow" ></a>
 </p>
export default {
  data() {
    return {
      dateRange: &#39;&#39;
    }
  },
  watch: {
    dateRange(newVal, oldVal) {
      console.log(newVal) // 选择日期后无法监听dateRange的改变
    }
  }
}
Copy after login

2. Analysis

Searching for information found that Vue is actually unable to monitor data changes caused by third-party plug-ins. So the above method doesn't work. However, Vue provides us with a method that can convert any data into data that can be monitored by Vue, which is: vm.$set.

3. Solution

Take the datepicker I use as an example (jquery-daterangepicker)

data() {
    return {
      date: &#39;&#39;,
      beginDate: &#39;&#39;,
      endDate: &#39;&#39;
    }
  },
mounted () {
  $(&#39;.daterangepicker&#39;).dateRangePicker({
    autoClose: true,
    format: &#39;YYYY-MM-DD&#39;
  }).bind(&#39;datepicker-change&#39;, this.setDate) //插件自带方法,选择日期后触发回调
 },
methods: {
  setDate() {
    let datepicker = this.$refs.datepicker
    //这一步是关键,具体说明可以参见vue api手册
    this.$set(this.date, &#39;beginDate&#39;, datepicker.value)
    this.$set(this.date, &#39;endDate&#39;, datepicker.value)
    this.beginDate = this.date.beginDate.slice(0, 11)
    this.endDate = this.date.endDate.slice(-10)
  }  
 },
  watch: {
  // 这里就可以监听数据变化啦,可以愉快的选择日期了!
   beginDate(newVal, oldVal) {
     this.$emit( &#39;beginDateChange&#39;, newVal )
   },
   endDate(newVal, oldVal) {
     this.$emit( &#39;endDateChange&#39;, newVal )
   }
  }
Copy after login

The above is what I compiled for everyone, I hope It will be helpful to everyone in the future.

Related articles:

How to implement the image preview function in the WeChat applet

How to implement the accordion special effect using jquery

How to use three-level linkage selectors in WeChat mini programs

PHP closures and anonymous functions (detailed tutorial)

About custom events in the Vue component (detailed tutorial)

How to use JS to implement the ball following the movement of the mouse

The above is the detailed content of The datepicker plug-in in Vue cannot monitor the value of the datepicker input box. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!