我有一个使用Vutify的v-工具提示的共享组件,当鼠标悬停时,它可以完美地工作.但是,当我使用轻击按钮访问带有工具提示的按钮时,它不起作用.有人能帮我解决这个问题吗?

以下是我的工具提示组件的代码:

<template>
  <v-tooltip
    v-bind="$attrs"
    :open-on-focus="true"
    :content-class="`${tooltipClass} custom-tooltip ${color} darken-2`"
    open-delay="250"
    max-width="288px"
  >
    <template v-slot:activator="{ on, attrs }">
      <div v-bind="attrs" v-on="on">
        <slot />
      </div>
    </template>
    <span class="tooltip-text">{{ getTooltipText() }}</span>
  </v-tooltip>
</template>

以下是我使用它的方法:

<tooltip
  tooltip-text="edit metadata"
  bottom
  position="bottom"
>
  <EditExternalButton
    :external-link="item.editLink"
    :data-test="`result-list-item-edit-external-${i + 1}`"
    event-name-string="metadata_edit clicked"
  />
</tooltip>

我试着加了 :open-on-focus="true"

在props 中,但它不起作用

推荐答案

普通的div通常不能被聚焦,所以你永远不会通过键盘到达它,处理程序也不会被触发.

有几种方法可以绕过它:

  • tabindex="0"添加到div以使其成为可选项卡:
<template v-slot:activator="{ on, attrs }">
  <div v-bind="attrs" v-on="on" tabindex="0">
    <slot />
  </div>
</template>

const tooltip = {
  template: `
    <v-tooltip
      v-bind="$attrs"
      :open-on-focus="true"
      open-delay="250"
      max-width="288px"
    >
      <template v-slot:activator="{ on, attrs }">
        <div v-bind="attrs" v-on="on" tabindex="0">
          <slot />
        </div>
      </template>
      <span class="tooltip-text">getTooltipText()</span>
    </v-tooltip>
  `
}

new Vue({
  el: '#app',
  vuetify: new Vuetify(),
  components: {tooltip},
  template: `
    <v-app>
      <v-main>
        <v-container>
          <tooltip>
            <div style="background: pink;">activator content</div>
          </tooltip>
        
        </v-container>
      </v-main>
    </v-app>
  `,
  setup(){
  
  }
})
<link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/@mdi/font@6.x/css/materialdesignicons.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.min.css" rel="stylesheet">

<div id="app"></div>

<script src="https://cdn.jsdelivr.net/npm/vue@2.x/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.js"></script>

  • 使用一个本身可选项卡的元素(如button)而不是div作为激活器:
<template v-slot:activator="{ on, attrs }">
  <button v-bind="attrs" v-on="on">
    <slot />
  </button>
</template>

const tooltip = {
  template: `
    <v-tooltip
      v-bind="$attrs"
      :open-on-focus="true"
      open-delay="250"
      max-width="288px"
    >
      <template v-slot:activator="{ on, attrs }">
        <v-btn v-bind="attrs" v-on="on">
          <slot />
        </v-btn>
      </template>
      <span class="tooltip-text">getTooltipText()</span>
    </v-tooltip>
  `
}

new Vue({
  el: '#app',
  vuetify: new Vuetify(),
  components: {tooltip},
  template: `
    <v-app>
      <v-main>
        <v-container>
          <tooltip>
            <div style="background: pink;">activator content</div>
          </tooltip>
        
        </v-container>
      </v-main>
    </v-app>
  `,
  setup(){
  
  }
})
<link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/@mdi/font@6.x/css/materialdesignicons.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.min.css" rel="stylesheet">

<div id="app"></div>

<script src="https://cdn.jsdelivr.net/npm/vue@2.x/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.js"></script>

  • 将与焦点相关的处理程序传递给槽内容,并在那里使用它:
<template v-slot:activator="{ attrs, on: {mouseenter, mouseleave, focus, blur, keydown} }">
  <div v-bind="attrs" v-on="{mouseenter, mouseleave}">
    <slot :on="{focus, blur, keydown}"/>
  </div>
</template>

const tooltip = {
  template: `
    <v-tooltip
      v-bind="$attrs"
      :open-on-focus="true"
      open-delay="250"
      max-width="288px"
    >
      <template v-slot:activator="{ attrs, on: {mouseenter, mouseleave, focus, blur, keydown} }">
        <div v-bind="attrs" v-on="{mouseenter, mouseleave}">
          <slot :on="{focus, blur, keydown}"/>
        </div>
      </template>
      <span class="tooltip-text">getTooltipText()</span>
    </v-tooltip>
  `
}

new Vue({
  el: '#app',
  vuetify: new Vuetify(),
  components: {tooltip},
  template: `
    <v-app>
      <v-main>
        <v-container>

          <tooltip v-slot="{on}">
            <v-checkbox v-on="on"/>
          </tooltip>
      
        </v-container>
      </v-main>
    </v-app>
  `,
  setup(){
  
  }
})
<link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/@mdi/font@6.x/css/materialdesignicons.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.min.css" rel="stylesheet">

<div id="app"></div>

<script src="https://cdn.jsdelivr.net/npm/vue@2.x/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.js"></script>

Vue.js相关问答推荐

检测Vue3/Vue-test-utils中的Vuex还原/Mutations

我需要在 nuxt2 上使用 /index 作为 url 的一部分

过渡如何在 Vue JS 3 中的图像上工作?

为什么普通对象在 Vue3 中自动变成响应式的?

:deep() 带有嵌套 scss 规则的语法

Vuetify 扩展面板,面板标题左侧带有图标

Vue:将点击事件绑定到动态插入的内容

v-for 内部的组件

Vue @click 不适用于存在 href 的anchor锚标记

如何有条件地更改 vue/vuetify 文本字段 colored颜色

是否可以在 vue-devtools 中命名 Vue 实例?

从我的服务访问路由实例

这是 v-on:change 的正确用法吗?

如何突出显示 Vuetify 菜单中的选定项目?

如何将事件绑定到 Vuetify 中的树视图 node ?

Vuetify 数据表不显示数据

beforeCreate 挂钩中的 Vue 2.1 调用方法不起作用

VueJS 复选框更新

为什么 router.currentRoute.path 没有react?

如何将单选按钮绑定到 vuex store ?