There is a similar question for Vue2 and recommendation is to use $options
.
But it seems that it does't work for Vue 3.
First of all, Vue 3 documentation say, that $options
is read only.
So when I am trying to initialize tooltip in instance when component mounted, I get very strange behavior, when tooltips are shown from last created component, so it seem that $options
are somehow "global" ?
When put tooptip
to data
everything works fine, but obviously tooltip should not be reactive and I'd like to put it outside the data
.
<template>
<i
:class="['bi ', icon, hover && 'text-primary']"
class="bg-body"
@mouseover="hover = true; $options.tooltip.show();"
@mouseleave="hover = false; $options.tooltip.hide();"
@click="$options.tooltip.hide();"
style="cursor: pointer"
:title="title"
ref="icon"
/>
</template>
<script>
import {Tooltip} from "bootstrap";
export default {
props: ["icon", "title"],
tooltip: null,
data() {
return {
hover: false
}
},
mounted() {
this.$options.tooltip = new Tooltip(this.$refs.icon,{
placement: 'bottom',
trigger: 'manual',
title: this.title || ''
});
},
}
</script>
CodePudding user response:
You can attach non-reactive properties directly to the component instance in the mounted()
hook:
<script>
export default {
// tooltip: null,
mounted() {
// this.$options.tooltip = new Tooltip(...)
this.tooltip = new Tooltip(...)
},
}
</script>
<template>
<!-- BEFORE -->
<i
@mouseover="hover = true; $options.tooltip.show();"
@mouseleave="hover = false; $options.tooltip.hide();"
@click="$options.tooltip.hide();"
ref="icon"
/>
<!-- AFTER -->
<i
@mouseover="hover = true; tooltip.show();"
@mouseleave="hover = false; tooltip.hide();"
@click="tooltip.hide();"
ref="icon"
/>
</template>