//TestPlugin.js const randomValue = (min, max) => { min = Math.ceil(min); max = Math.floor(max); const random = Math.floor(Math.random() * (max - min + 1)) + min; console.log(random); }; export default { install(Vue) { Vue.config.globalProperties.$randomValue = randomValue; }, }; //App.vue <template> <button>event</button> </template> Este código es un ejemplo. Quiero llamar a la función randomValue dentro de <script setup> y ponerla en un botón en lugar de <buton@click="$randomValue(0, 20)">
El siguiente es un ejemplo de lo que quiero hacer.
//App.vue <template> <button @click="random">event</button> </template> <script setup> const random = $randomValue(0,10); </script>¿Cómo hago esto?
Encontré la respuesta yo mismo...
//TestPlugin.js export default { install(Vue) { Vue.config.globalProperties.$randomValue = randomValue; Vue.config.globalProperties.$valueCheck = valueCheck; Vue.provide("plugins", { valueCheck, randomValue }); }, }; //App.vue <script setup> import { inject } from 'vue'; const { valueCheck, randomValue } = inject("plugins"); const hello = () => { randomValue(0,10); } </script> Vue.provide("plugins", { valueCheck, randomValue }); e import { inject } from 'vue'; const { valueCheck, randomValue } = inject("plugins");