如何在Vue.js中添加bootstrap工具提示

Sar*_* TS 27 twitter-bootstrap laravel vue.js

我有一个页面,用于使用Vue.js和Laravel列出表中的数据.上市数据是成功的.删除和编辑功能正在进行中.为此,我增加了两个<span> (glyphicon-pencil), <span> (glyphicon-trash).如果两者<span>都在<template>工具提示显示之外,否则它不起作用.你知道引导工具提示在Vue Js中是如何工作的吗?谢谢.

page.blade.php

    <template id="tasks-template">
       <table class="table table-responsive table-bordered table-hover">
            <thead>
                   <tr>
                   <th>#</th>
                   <th>Id</th>
                   <th>Religion</th>
                   <th>Action</th>
                   <th>Created</th>
                   <td>Status</td>
               </tr>
           </thead>

      <tbody>
             <tr v-for="(index, task) in list">
             <td><input type="checkbox" id="checkbox" aria-label="checkbox" value="checkbox"></td>
             <td>@{{ index + 1 }}</td>
            <td>@{{ task.religion | capitalize }}</td>
           <td v-if="task.status == 'publish'">
                     <span class="glyphicon glyphicon-ok"></span>
           </td>
           <td v-else>
                     <span class="glyphicon glyphicon-remove"></span>
           </td>
           <td>@{{ task.created_at }}</td>
           <td>
               <span class="glyphicon glyphicon-pencil" aria-hidden="true" data-toggle="tooltip" data-placement="left" title="Edit"></span> 
               <span class="glyphicon glyphicon-trash" aria-hidden="true" data-toggle="tooltip" data-placement="right" title="Delete"></span>
           </td>
         </tr>
       </tbody>
        </table>
        </template>

        <tasks></tasks> 
@push('scripts')
    <script src="/js/script.js"></script>
@endpush 
Run Code Online (Sandbox Code Playgroud)

scripts.js中

$(function () {
    $('[data-toggle="tooltip"]').tooltip()
})


Vue.component('tasks', {

    template: '#tasks-template',

    data: function(){
        return{
            list: []
        };
    },

    created: function(){
        this.fetchTaskList();
    },

    methods: {
        fetchTaskList: function(){
            this.$http.get('/backend/religion/data', function(tasks){
                this.$set('list', tasks);
            });
        }
    }

});

new Vue({
   el: 'body'
});
Run Code Online (Sandbox Code Playgroud)

Ikb*_*bel 73

您可以使用此指令:

Vue.directive('tooltip', function(el, binding){
    $(el).tooltip({
             title: binding.value,
             placement: binding.arg,
             trigger: 'hover'             
         })
})
Run Code Online (Sandbox Code Playgroud)

例如:

<span class="label label-default" v-tooltip:bottom="'Your tooltip text'">
Run Code Online (Sandbox Code Playgroud)

或者您也可以将工具提示文本绑定到计算变量:

<span class="label label-default" v-tooltip:bottom="tooltipText">
Run Code Online (Sandbox Code Playgroud)

在您的组件脚本中:

computed: {
    tooltipText: function() {
       // put your logic here to change the tooltip text
       return 'This is a computed tooltip'
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 这应该是公认的答案.纯粹的自助和直截了当.不要使用外部库 (12认同)
  • 你怎么能破坏工具提示`$(el).tooltip('destroy')`? (2认同)
  • 如果使用打字稿,请务必安装 npm 包 @types/jquery (2认同)

Jef*_*eff 19

您需要在$('[data-toggle="tooltip"]').tooltip()从服务器加载数据后运行.要确保更新DOM,您可以使用以下nextTick函数:

fetchTaskList: function(){
    this.$http.get('/backend/religion/data', function(tasks){
        this.$set('list', tasks);
        Vue.nextTick(function () {
            $('[data-toggle="tooltip"]').tooltip()
        })
    });
}
Run Code Online (Sandbox Code Playgroud)

https://vuejs.org/api/#Vue-nextTick

编辑:Vitim.us发布了一个更完整,更强大的解决方案

  • 由于一些原因,这不是正确的方法,如果 DOM 做出反应并重新渲染视图的一部分,这可能会中断。您不会在初始化后破坏工具提示。这可能会中断,因为 nextTick 是异步的,并且 render 和 nextTick 之间的某些内容可以更改您的 DOM 状态。 (4认同)

Mar*_*der 11

我尝试使用 Vitim.us 发布的解决方案,但遇到了一些问题(意外/未设置的值)。这是我的固定和缩短版本:

import Vue from 'vue'

const bsTooltip = (el, binding) => {
  const t = []

  if (binding.modifiers.focus) t.push('focus')
  if (binding.modifiers.hover) t.push('hover')
  if (binding.modifiers.click) t.push('click')
  if (!t.length) t.push('hover')

  $(el).tooltip({
    title: binding.value,
    placement: binding.arg || 'top',
    trigger: t.join(' '),
    html: !!binding.modifiers.html,
  });
}

Vue.directive('tooltip', {
  bind: bsTooltip,
  update: bsTooltip,
  unbind (el) {
    $(el).tooltip('dispose')
  }
});
Run Code Online (Sandbox Code Playgroud)

要将它与 Nuxt.js 一起使用,您可以创建一个插件:

将上面的代码放在一个文件中,例如/plugins/bs-tooltips.js并将其注册到您的nuxt.config.js.

plugins: [
    '~/plugins/bs-tooltips.js'
],
Run Code Online (Sandbox Code Playgroud)

现在这有效:

<button v-tooltip="'Tooltip text'">Hover me</button>
<button v-tooltip.click="Tooltip text">Click me</button>
<button v-tooltip.html="Tooltip text">Html</button>
<button v-tooltip:bottom="Tooltip text">Bottom</button>
<button v-tooltip:auto="Tooltip text">Auto</button>
Run Code Online (Sandbox Code Playgroud)

  • 这是最好的答案! (2认同)

Vit*_*.us 8

正确的方法是使它成为一个指令,这样你就可以挂钩DOM元素的生命周期.

https://vuejs.org/v2/guide/custom-directive.html

/**
 * Enable Bootstrap tooltips using Vue directive
 * @author Vitim.us
 * @see https://gist.github.com/victornpb/020d393f2f5b866437d13d49a4695b47
 * @example
 *   <button v-tooltip="foo">Hover me</button>
 *   <button v-tooltip.click="bar">Click me</button>
 *   <button v-tooltip.html="baz">Html</button>
 *   <button v-tooltip:top="foo">Top</button>
 *   <button v-tooltip:left="foo">Left</button>
 *   <button v-tooltip:right="foo">Right</button>
 *   <button v-tooltip:bottom="foo">Bottom</button>
 *   <button v-tooltip:auto="foo">Auto</button>
 *   <button v-tooltip:auto.html="clock" @click="clock = Date.now()">Updating</button>
 *   <button v-tooltip:auto.html.live="clock" @click="clock = Date.now()">Updating Live</button>
 */
Vue.directive('tooltip', {
  bind: function bsTooltipCreate(el, binding) {
    let trigger;
    if (binding.modifiers.focus || binding.modifiers.hover || binding.modifiers.click) {
      const t = [];
      if (binding.modifiers.focus) t.push('focus');
      if (binding.modifiers.hover) t.push('hover');
      if (binding.modifiers.click) t.push('click');
      trigger = t.join(' ');
    }
    $(el).tooltip({
      title: binding.value,
      placement: binding.arg,
      trigger: trigger,
      html: binding.modifiers.html
    });
  },
  update: function bsTooltipUpdate(el, binding) {
    const $el = $(el);
    $el.attr('title', binding.value).tooltip('fixTitle');

    const data = $el.data('bs.tooltip');
    if (binding.modifiers.live) { // update live without flickering (but it doesn't reposition)
      if (data.$tip) {
        if (data.options.html) data.$tip.find('.tooltip-inner').html(binding.value);
        else data.$tip.find('.tooltip-inner').text(binding.value);
      }
    } else {
      if (data.inState.hover || data.inState.focus || data.inState.click) $el.tooltip('show');
    }
  },
  unbind(el, binding) {
    $(el).tooltip('destroy');
  },
});


//DEMO
new Vue({
  el: '#app',
  data: {
    foo: "Hi",
    bar: "There",
    baz: "<b>Hi</b><br><i>There</i>",
    clock: '00:00',
  },
  mounted() {
    setInterval(() => this.clock = new Date().toLocaleTimeString(), 1000);
  }
});
Run Code Online (Sandbox Code Playgroud)
<link href="https://unpkg.com/bootstrap@3.3.7/dist/css/bootstrap.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://unpkg.com/bootstrap@3.3.7/dist/js/bootstrap.js"></script>
<script src="https://unpkg.com/vue@2.5.16/dist/vue.js"></script>


<div id="app">
  <h4>Bootstrap tooltip with Vue.js Directive</h4>
  <br>
  <button v-tooltip="foo">Hover me</button>
  <button v-tooltip.click="bar">Click me</button>
  <button v-tooltip.html="baz">Html</button>
  <br>
  <button v-tooltip:top="foo">Top</button>
  <button v-tooltip:left="foo">Left</button>
  <button v-tooltip:right="foo">Right</button>
  <button v-tooltip:bottom="foo">Bottom</button>
  <button v-tooltip:auto="foo">Auto</button>

  <button v-tooltip:auto.html="clock" @click="clock = 'Long text test <b>bold</b>'+Date.now()">Updating</button>

  <button v-tooltip:auto.html.live="clock" @click="clock = 'Long text test  <b>bold</b>'+Date.now()">Updating Live</button>
</div>
Run Code Online (Sandbox Code Playgroud)

  • 良好彻底的解决方案 (2认同)
  • 在Bootstrap 4中,它现在是```dispose```而不是```destroy``` (2认同)

SHA*_*ana 5

在vuejs中使用引导工具提示的简便方法

安装boostrap,jquery和popper.js

对于jquery,bootstrap和popper.js,在main.js中添加以下代码

import 'popper.js'
import 'bootstrap/dist/css/bootstrap.min.css'
import 'bootstrap/dist/js/bootstrap.min.js'
import jQuery from 'jquery'
//global declaration of jquery
global.jQuery = jQuery
global.$ = jQuery

$(() => {
  $('#app').tooltip({
    selector: '[data-toggle="tooltip"]'
  })
})
Run Code Online (Sandbox Code Playgroud)

如果您在vuejs中使用eslint,请不要忘记在.eslintrc.js文件中添加以下代码

env: {
  browser: true,
  "jquery": true
}
Run Code Online (Sandbox Code Playgroud)

而且不要忘记重新编译vuejs