Is there a way to use $emit in boot function defintion? #14343
Replies: 1 comment 1 reply
-
|
https://v3-migration.vuejs.org/breaking-changes/events-api.html#_3-x-update first of all, u can write a dumb inject helper to use in your boots: import { App, InjectionKey } from 'vue';
export function inject<T>(app: App, key: InjectionKey<T>): T {
return app._context.provides[key as never] as T;
}so, create a boot who register the emitter, this boot would be called before the boots who requires the emitter. import { boot } from 'quasar/wrappers';
import { InjectionKey } from 'vue';
import mitt, { Emitter } from 'mitt';
type Events = {
foo: string;
bar?: number;
};
export const emitterKey: InjectionKey<Emitter<Events>> = Symbol('emitter-key');
export default boot(({ app }) => {
const emitter: Emitter<Events> = mitt<Events>();
app.provide(emitterKey, emitter);
});in other boots: import { emitterKey } from './emitter'
import { boot } from 'quasar/wrappers';
import { inject } from 'src/utils';
export default boot(({ app }) => {
const emitter = inject(app, emitterKey)
emitter.on('foo', function (val: string) {
console.log(val)
})
emitter.on('bar', function (val: number | undefined) {
console.log(val)
})
});so, in your components: import { emitterKey } from 'src/boot/emitter'
import { defineComponent, inject } from 'vue';
export default defineComponent({
setup () {
const emitter = inject(emitterKey)
emitter.on('foo', function (val: string) {
console.log(val)
})
emitter.on('bar', function (val: number | undefined) {
console.log(val)
})
}
}) |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
This is related to the $emit on app extension forum post.
Essentially, is there a way to invoke the
emit()function from within a boot file?Beta Was this translation helpful? Give feedback.
All reactions