'How to inject JavaScript into Vue script

I have a block of code that needs to be reused in multiple components. What is the best way to do this in Vue 3 with the composition API and script setup? I've looked into composables, but I can't get it to work for my use case. My code is a bit like this

const popupEnabled = ref(false);

function currentPopupState() {
    return store.getters.getPopupState;
}

onMounted(() => {
    watch(currentPopupState, (value) => {
        if (value) {
            popupEnabled.value = true;
            setTimeout(() => {
                store.commit("changeCurrentPopupState", false);
                popupEnabled.value = false;
            }, 1800);
        }
    });
});

It watches an external value, then runs a timed function if the value is changed. I just want to inject this block of code into my Vue script, how do I do this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source