最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Vue.js mixins call parent method in overridden implementation - Stack Overflow

programmeradmin3浏览0评论

I'm using vuejs-datepicker component in a project however I need some custom behaviour that's why I decided to create my own datepicker and inject vuejs-datepicker as a mixin. The solution works fine but I'm looking for a way to call the parent method inside my overridden one. That's how my component looks for now:

  import Datepicker from 'vuejs-datepicker'

  export default {
    props: {
      /**
       * My custom property startDate to open a calendar on the given date by default
       */
      startDate: {
        validator: function (val) {
          return val === null || val instanceof Date || typeof val === 'string'
        },
        default: new Date()
      }
    },

    mixins: [ Datepicker ],

    methods: {
      /**
       * I override parent method setPageDate to read default startDate when none is specified
       */
      setPageDate (date) {
        // my code to set default date from the property
        if (!date) {
          date = this.startDate
        }

        // this part is the same as in the original method
        this.pageDate = new Date(date.getFullYear(), date.getMonth(), 1, date.getHours(), date.getMinutes()).getTime()
      }
    }
  }

Just one line of copied code isn't a big deal but I expect I need to override more methods in the future. So I'm looking for the best way to call that parent method inside my implementation, something like this.parent(date) or this.super(date). Is it possible at all?

I'm using vuejs-datepicker component in a project however I need some custom behaviour that's why I decided to create my own datepicker and inject vuejs-datepicker as a mixin. The solution works fine but I'm looking for a way to call the parent method inside my overridden one. That's how my component looks for now:

  import Datepicker from 'vuejs-datepicker'

  export default {
    props: {
      /**
       * My custom property startDate to open a calendar on the given date by default
       */
      startDate: {
        validator: function (val) {
          return val === null || val instanceof Date || typeof val === 'string'
        },
        default: new Date()
      }
    },

    mixins: [ Datepicker ],

    methods: {
      /**
       * I override parent method setPageDate to read default startDate when none is specified
       */
      setPageDate (date) {
        // my code to set default date from the property
        if (!date) {
          date = this.startDate
        }

        // this part is the same as in the original method
        this.pageDate = new Date(date.getFullYear(), date.getMonth(), 1, date.getHours(), date.getMinutes()).getTime()
      }
    }
  }

Just one line of copied code isn't a big deal but I expect I need to override more methods in the future. So I'm looking for the best way to call that parent method inside my implementation, something like this.parent(date) or this.super(date). Is it possible at all?

Share Improve this question asked Aug 12, 2017 at 19:31 ConstantineUAConstantineUA 1,0511 gold badge9 silver badges19 bronze badges 1
  • Looks like there's a library for this: dev.to/ashraful/call-super-method-in-vue-component-3pl2 – diachedelic Commented Mar 10, 2020 at 23:14
Add a comment  | 

1 Answer 1

Reset to default 24

There is no super or anything like that. You could do something like this:

  setPageDate (date) {
    // my code to set default date from the property
    if (!date) {
      date = this.startDate
    }

    Datepicker.methods.setPageDate.call(this, date)
  }
发布评论

评论列表(0)

  1. 暂无评论