-
Notifications
You must be signed in to change notification settings - Fork 343
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Document that onMounted sets this
#206
Comments
@carlmjohnson thanks for the workaround! Was scratching my head over this one as well. It doesn't feel right using |
Since version <template>
<div>
<span>{{ counter }}</span>
<button @click="increment">increment</button>
<button @click="decrement">decrement</button>
</div>
</template>
<script>
import Vue from 'vue'
import VueCompositionApi from '@vue/composition-api'
Vue.use(VueCompositionApi)
import { getCurrentInstance } from '@vue/composition-api'
function useEvents () {
const vm = getCurrentInstance()
return {
on: (event, callback) => vm.$on(event, callback),
once: (event, callback) => vm.$once(event, callback),
off: (event, callback) => vm.$off(event, callback),
emit: (event, ...args) => vm.$emit(event, ...args)
}
}
import { ref } from '@vue/composition-api'
export default {
setup () {
const counter = ref(0)
const { on, emit } = useEvents()
on('increment', () => {
counter.value = counter.value + 1
})
on('decrement', () => {
counter.value = counter.value - 1
})
return {
counter,
increment () {
emit('increment')
},
decrement () {
emit('decrement')
}
}
}
}
</script>
|
Great! It needs to be added to https://vue-composition-api-rfc.netlify.com/api.html. |
@carlmjohnson - a few things from the referenced PR
|
setup(_, ctx){
ctx.emit('change', 1)
} I'm going to close this issue, if still more questions, please reopen or create another issue. |
On Reddit, a question came up about how to do
this.$on(event, cb);
using the composition API. It had me scratching my head for a long time, but eventually I found out thatonMounted
if called with a function (not an arrow func) will setthis
to the component instance, at which point you can just callthis.$on
as usual.I think that
a) the fact that
this
is set should be documented if intended or removed if notb) it should be better documented how to add programmatic listeners with the composition API, assuming this is the intended method.
The text was updated successfully, but these errors were encountered: