在v-for循环中使用vue组件

时间:2017-06-28 14:25:09

标签: javascript laravel laravel-5.3 vuejs2 vue-component

您好我需要帮助生成多个vue组件(vue-popperjs - for tooltip)。

在下面的代码中,组件出现在页面上,但我无法与它们进行交互(按钮不会切换工具提示和工具提示默认显示)并且我在控制台中出错 " vue.common.js?e881:560 [Vue警告]:挂钩错误: (找到)"

TypeError: Cannot read property '0' of undefined
    at VueComponent.mounted (eval at  (app.js:909), :117:64)
    at callHook (eval at  (app.js:891), :2275:21)
    at Object.insert (eval at  (app.js:891), :2954:7)
    at invokeInsertHook (eval at  (app.js:891), :4960:28)
    at Vue$3.patch [as __patch__] (eval at  (app.js:891), :5124:5)
    at Vue$3.Vue._update (eval at  (app.js:891), :2042:19)
    at Vue$3.updateComponent (eval at  (app.js:891), :2158:10)
    at Watcher.get (eval at  (app.js:891), :2469:25)
    at Watcher.run (eval at  (app.js:891), :2539:22)
    at flushSchedulerQueue (eval at  (app.js:891), :2331:13)

代码:

<span class="input" v-for="additionalCategory in additionalCategories">
  <p>@{{ additionalCategory.name }}

    <popper trigger="click" :options="{placement: 'right'}" :content="additionalCategory.tooltip_text">
      <div class="additionalCategory.tooltip_text">
        @{{ additionalCategory.tooltip_text }}
      </div>

      <button slot="reference">
        Reference Element
      </button>
    </popper>
    <span v-if="additionalCategory.tooltip_active" class="tooltip-div" @click="showTooltip(additionalCategory.tooltip_text, additionalCategory.id, $event)">

      <span class="tooltip_toggle" v-bind:class="additionalCategory.id"></span>
    </span>
  </p>

  <div class="input-action">
    <div class="counter">
      <input type="text" :value="additionalCategory.quantity" :id="additionalCategory.id">

      <div class="ico"><img :src="'/images/icons/'+additionalCategory.icon+'.png'"
                            :id="additionalCategory.name"></div>
      <div class="plus-minus">
        <div class="minus" v-on:click="clickMinus($event, additionalCategory.id)"></div>
        <div class="plus" v-on:click="clickPlus($event, additionalCategory.id)"></div>
      </div>
    </div>
  </div>
</span>

我在app.js注册了popper:

Vue.component('popper', require('vue-popperjs'));

并且作为相同的组件工作但不在for循环中。 请帮助我,我不知道该怎么做。

1 个答案:

答案 0 :(得分:2)

这里的问题归结为嵌入在段落元素(popper)中的<p></p>组件。从该元素中取出它解决了这个问题。