element ui 時間選擇器pickeroption里面this的指向問題


pickerOptions: {
        disabledDate(time) {
          console.log(this) //undefined 拿不到vue實例
          console.log(_this.searchForm.timeValue)//可以拿到值
          console.log(_this) //vue實例
          const lastYearDate = new Date(getLastFormatYear(time)).getTime()
          //  console.log(lastYearDate,8888)
          return time.getTime() > new Date() || time.getTime() < lastYearDate
        }
      }
disabledDate里面拿不到vue實例本身的,因為data是一個函數,pickerOptions屬於函數里的一個屬性,有關this和作用域的問題,這里需要增加this的指向問題

解決方法一:可以在data里拿一下this:

data() {
    var _this = this
    return {
    .....
    }    

解決方法二:可以在computed寫一個方法

computed: {
    pickerOptions() {
      // 用計算屬性
      let _this = this
      // 此時 this指向的就是vue實例
      return {
        disabledDate(time) {
          console.log(_this)//這里可以拿到this了
          return time.getTime() > Date.now()
        }
      }
    }
  }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM