npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mitt-vue

v1.1.1

Published

Lightweight utility for integrating mitt with Vue 2 and Vue 3

Downloads

39

Readme

Mitt Vue

mitt-vue is a package for handling events in Vue 2 and Vue 3 applications using the mitt library. It provides a simple API for emitting and listening to events in your Vue components. (similar to the package mitt-react)

This package offers a function that automatically handles event subscription and unsubscription in the lifecycke hooks mounted / destroyed (Vue 2), onMounted / onUnmounted or mounted / unmounted (Vue 3) This simplifies the process of managing event listeners in Vue components, ensuring they are properly set up and cleaned up to avoid memory leaks.

Installation

npm install mitt-vue

Features

useEventListener

The useEventListener function allows you to listen to custom events in your Vue components. This will automatically create a suscription an unsuscription for the event in the component.

useEventListener('customEvent', (data) => {
  setMessage(data);
});

useEventEmit

The useEventEmit function allows you to emit custom events.

useEventEmit('customEvent', 'Hello, World!');

Usage

Vue 2

For Vue 2, use mixins to manage event subscription and unsubscription.

<template>
  <div id="app">
    <button @click="emitEvent">Emit Event</button>
    <p>{{ message }}</p>
  </div>
</template>

<script>
import { useEventEmit, useEventListener } from 'mitt-vue';

export default {
  name: 'App',
  data() {
    return {
      message: 'Waiting for event...',
    };
  },
  mixins: [
    useEventListener('my-event', (data) => {
      this.message = `Event received with data: ${JSON.stringify(data)}`;
    }),
  ],
  methods: {
    emitEvent() {
      useEventEmit('my-event', { foo: 'bar' });
    },
  },
};
</script>

Vue 3

For Vue 3, use the any API (Options or Composition) to manage event subscription and unsubscription. The function useEventListener works both for Options and Composition API, so you can use <script setup> perfectly.

<template>
  <div id="app">
    <button @click="emitEvent">Emit Event</button>
    <p>{{ message }}</p>
  </div>
</template>

<script>
import { ref } from 'vue';
import { useEventEmit, useEventListener } from 'mitt-vue';

export default {
  name: 'App',
  setup() {
    const message = ref('Waiting for event...');

    useEventListener('my-event', (data) => {
      message.value = `Event received with data: ${JSON.stringify(data)}`;
    });

    function emitEvent() {
      useEventEmit('my-event', { foo: 'bar' });
    }

    return {
      message,
      emitEvent,
    };
  },
};
</script>

API

useEventListener

A function to listen for a custom event.

| Param | Type | Nullable | Desc | | --------- | -------- | -------- | ----------------------------------------------- | | eventName | string | ✗ | The name of the event to listen for | | callback | Function | ✗ | The function to call when the event is emitted. |

useEventEmit

A function to emit a custom event.

| Param | Type | Nullable | Desc | | --------- | ------ | -------- | --------------------------------------- | | eventName | string | ✗ | The name of the event to emit. | | data | any | ✗ | The data to pass to the event callback. |

Contribution

Contributions are welcome! Please open an issue or submit a pull request on GitHub.

License

This project is licensed under the MIT License.