Skip to content

Instantly share code, notes, and snippets.

@chrisyip
Created Nov 13, 2016
Embed
What would you like to do?
Vue v2 helpful plugins
Vue.components('my-component', {
  ready () {
    this.doSomething()
  }
})

equals

Vue.components('my-component', {
  mounted () {
    this.$nextTick(() => this.doSomething())
  }
})

===

Vue.components('my-component', {
  methods: {
    onClick () {
      this.$refs.child.$emit('fire-click')
    }
  }
})

Vue.components('child-component', {
  events: {
    'fire-click' () {}
  }
})
export default {
install: function (Vue) {
Vue.mixin({
created () {
const events = this.$options.events
if (events) {
Object.keys(events).forEach(event => {
this.$on(event, events[event].bind(this))
})
}
},
mounted () {
if (this.$options.ready) {
this.$nextTick(() => this.$options.ready.call(this))
}
}
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment