I want to switch from Options API to Composition API and use composables in place of mixin. So far I've been using a global mixin with computed property like this:
// globalMixin.js
computed: {
myAlert() {
return this.$refs.myAlertDiv;
}
}
Then I used this mixin when creating app:
// MyApp.js
const MyApp = {
mixins: [globalMixin]
...
}
myAlert became MyApp's computed property and I could use it without declaring directly inside MyApp properties.
Now I want to achieve the same result with composables, let's say I have a component importing a composable:
// App.vue
<script setup>
import { useGlobalComposable} from './globalComposable.js';
const globalComposable = useGlobalComposable();
onMounted(() => {
// should work without declaring myAlert inside App.vue
console.log(globalComposable.myAlert);
})
...
</script>
Is it possible to do? If so, how should I declare myAlert template ref inside composable?
Your
useGlobalComposable
function should returnmyAlert
, like this:Then you can declare myAlert in the setup block
Note, that
this.$refs
from themixin
will not work directly with the Composition API. If you create a component, then you can usethis
to access the component properties and methods.Here is a good article about using
refs
with Composition API.Very simple working example of using a composable in
setup
: