Skip to content
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

Nuxt3 issues ? #160

Open
bloor opened this issue May 2, 2022 · 2 comments
Open

Nuxt3 issues ? #160

bloor opened this issue May 2, 2022 · 2 comments

Comments

@bloor
Copy link

bloor commented May 2, 2022

I`m struggling to make it work with a nuxt3 project (static , ssr:false).

Basically it DOES emit the events, but it's harder to receive them.
I added this in: current component , sub-component, sub-sub-component
emitter.on('*', () => { alert(1)});

And only the one in the current component is triggered. Don't seem to be able to go cross-components. Might be because of how nuxt encapsulates the components _ .. but currently I have no easy solution for "global" events..

@bloor
Copy link
Author

bloor commented May 2, 2022

I actually made it work, BUT integrating it as a Nuxt Plugin, following this
https://github.com/nuxt/framework/discussions/2288#discussioncomment-2204699

Guess being bundled as a plugin it has more reach between components.

@rrindels
Copy link

I actually prefer a Nuxt3 Composable for this instead of a plugin. @/composables/useMitt.ts

import mitt from 'mitt'
interface LogMessage{
severity: string,
message: string
}
type ApplicationEvents = {
'application:log' : LogMessage
};
const emitter = mitt<ApplicationEvents>()
emitter.on('application:log' , ( type , message ) => console.log( type, message ))
export const sendEvent = emitter.emit
export const useListen = emitter.on

Then in your components sendEvent and useListen are just available.

Example of sendEvent in Component
<script setup lang="ts">
sendEvent('application:log',{ severity: 'Critical', message: 'Oh No It Failed'})
</script>

Example of useListen in Component
<script setup lang="ts">
useListen('application:log', async (logMessage) =>{
// Do something with logMessage like send to a northbound server log.
await postToServer( 'LogMessage' , logMessage.severity , logMessage.message );
};
</script>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants