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' () {}
}
})
Created
November 13, 2016 18:47
-
-
Save chrisyip/9f0375177061c18ee842bd88e45a9974 to your computer and use it in GitHub Desktop.
Vue v2 helpful plugins
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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