Skip to content

Instantly share code, notes, and snippets.

@chrisyip
Created November 13, 2016 18:47
Show Gist options
  • Save chrisyip/9f0375177061c18ee842bd88e45a9974 to your computer and use it in GitHub Desktop.
Save chrisyip/9f0375177061c18ee842bd88e45a9974 to your computer and use it in GitHub Desktop.
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