【问题标题】:How to use v-tooltip with v-file-input tag in Vuetify 2.0?如何在 Vuetify 2.0 中使用带有 v-file-input 标签的 v-tooltip?
【发布时间】:2021-04-29 22:44:27
【问题描述】:

我想使用 v-tooltip,当我通过 v-file-input 输入文件并将鼠标悬停在文件名上时, 文件名将像 v-tool-tip 一样弹出。 所以我尝试使代码如下。

<template>
  <v-row>
    <v-col cols="4">file_Add_Sample_Code</v-col>
    <v-col cols="6" class="pl-0 py-2">
      <v-tooltip bottom v-model="showTooltip">
        <template v-slot:activator="{ on, attrs }">
          <v-file-input
            accept="application/zip"
            v-model="fileName"
            @change="getFileObject"
            truncate-length="22"
            style="flex-direction: row-reverse"
            v-bind="attrs"
            v-on="on"
            @mouseover="showTooltip = !showTooltip"
          >
          </v-file-input>
        </template>
        <span>{{ fileName }}</span>
      </v-tooltip>
    </v-col>
    <v-col cols="2" class="pl-0"></v-col>
  </v-row>
</template>

<script lang="ts">
import { Component, Vue } from 'nuxt-property-decorator'
@Component({})
export default class extends Vue {
  showTooltip: boolean = false
  fileName: string = ''
  async getFileObject(file:File) {
    this.fileName = await file.name
  }
}
</script>

我运行此代码并输入文件,文件输入成功,工具提示显示但从未消失。 所以,我认为在我的代码中使用 JS 中的事件处理程序谎言 @mouseover 是正确的我的问题,但它似乎不起作用。 我的目标是当我将鼠标悬停在文件输入标签上,然后工具提示显示为Vuetify's tooltip sample 有人给我建议吗?

【问题讨论】:

  • 您是否有理由不使用v-file-input 的正常功能在控件中显示文件名?
  • 感谢cmets,抱歉回复晚了!在我最新的项目中,我需要获取文件对象以将其放入 RestAPI。所以,我在我的代码中使用了“getFileObject”方法。对不起,缺少信息。

标签: javascript typescript vue.js vuetify.js


【解决方案1】:

来自您的代码:

<v-file-input
  ...
  v-on="on"
  @mouseover="showTooltip = !showTooltip"
  >
</v-file-input>

v-on="on" 的原因仅在单击而不是悬停时有效,因为 v-file-input 仅发出 focus 和 blur 事件,但不会发出 mouseenter、mouseleave 或 mouseover 事件。

由于v-file-input 不会发出mouseover 事件,您的showTooltip = !showTooltip 代码实际上不会被执行。

你可以使用native修饰符来解决这个问题:

<v-file-input
  ...
  @mouseenter.native="on.mouseenter"
  @mouseleave.native="on.mouseleave"
  >
</v-file-input>

Example

【讨论】:

  • 我很清楚为什么我的代码不起作用。我需要添加原生修饰符才能使用我使用的 evet。我觉得我需要了解 vue.js 组件。非常感谢您的回答!这对我很有帮助!
  • 我在 Jsfiddle 和我的开发环境中确认了您的示例 conde。我对您的代码有一个问题。为什么像“on.mouseenter”和“on.mouseleave”这样的方法定义为事件处理程序的方法,mouseenter和mouseleave事件会起作用?
  • 对不起,有线问题,但我的第一个载体是从 Vue.js 开始的,所以我缺乏关于 DOM 和 HTML 的知识。如果你有时间,你能告诉我吗?
  • @drunkdolphin 我发现了这个answer,它很好地解释了激活剂。
  • 见v-on。语法v-on='someObject' this 称为对象语法,不支持任何修饰符。所以我使用@mouseenter.native="on.mouseenter"绑定原生事件。
猜你喜欢
  • 1970-01-01
  • 2020-01-24
  • 2022-08-22
  • 1970-01-01
  • 2020-04-09
  • 1970-01-01
  • 2021-02-09
  • 2020-06-18
  • 2022-11-21
相关资源
最近更新 更多