[Vuejs]-VueJS define global function for child components

3👍

You can use provide/inject, first provide your function to your child components from the app (or parent component)

const Modal = {...}
const app = createApp({})
app.provide('Modal', Modal)

Then inject it into your component

import { inject } from 'vue'

export default {
  setup() {
    const Modal = inject('Modal')
    return { Modal }
  }
}

Or via script setup:

<script setup>
import { inject } from "vue";
const Modal = inject("Modal");
</script>

Leave a comment