如何在vue组件上完成循环后调用语句或方法?

时间:2017-07-20 05:22:36

标签: vue.js vuejs2 vue-component

我的vue组件是这样的:

<template>
    <div class="row">
        <div class="col-md-3" v-for="item in items">
            ...
        </div>
    </div>
</template>
<script>
    export default {
        ...
        computed: {
            items() {
                ...
            }
        },
        ...
    }
</script>

如果循环完成,我想调用一个语句或方法

因此语句在循环完成时执行

我该怎么做?

更新:

从Kira San回答,我尝试这样:

  <template>
        <div class="row">
            <div class="col-md-3" v-for="(item, key) in items" v-for-callback="{key: key, array: items, callback: callback}">
                ...
            </div>
        </div>
    </template>
    <script>
        export default {
            ...
            computed: {
                items() {
                    const n = ...
                    return n
                }
            },
            directives: {
              forCallback(el, binding) {
                  let element = binding.value
                  if (element.key == element.array.length - 1)
                  if (typeof element.callback === 'function') {
                    element.callback()
                  }
                }
            },
            methods: {
                callback() {
                    console.log('v-for loop finished')          
                }
            }
        }
    </script>

控制台日志不显示

我的项目是对象

如果在项目中执行console.log(n),结果如下:

enter image description here

3 个答案:

答案 0 :(得分:1)

看看这个例子。

new Vue({
  el: '#app',

  computed: {
    items() {
      return {item1: 'value1', item2: 'value2'}
    }
  },

  methods: {
    callback() {
      console.log('v-for loop finished')
    }
  },

  directives: {
    forCallback(el, binding) {
      let element = binding.value
      var key = element.key
      var len = 0

      if (Array.isArray(element.array)) {
        len = element.array.length
      }

      else if (typeof element.array === 'object') {
        var keys = Object.keys(element.array)
        key = keys.indexOf(key)
        len = keys.length
      }

      if (key == len - 1) {
        if (typeof element.callback === 'function') {
          element.callback()
        }
      }
    }
  },

})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.3.4/vue.min.js"></script>

<div id="app">
    <div class="row">
        <div class="col-md-3" v-for="(item, key) in items" v-for-callback="{key: key, array: items, callback: callback}">
            ...
        </div>
    </div>
</div>

答案 1 :(得分:0)

您可以使用directive完成此操作。

使用示例:

<div class="col-md-3" v-for="(item, key) in items" v-for-callback="{key: key, array: items, callback: callback}">
  <!--content-->
</div>

创建一个新指令for-callback,它将跟踪使用v-for呈现的元素。它基本上会检查当前key是否是array的结尾。如果是这样,它将执行您提供的callback功能。

Vue.directive('for-callback', function(el, binding) {
  let element = binding.value
  if (element.key == element.array.length - 1)
    if (typeof element.callback === 'function') {
      element.callback()
    }
})

或者如果您不想全局定义它。将其添加到您的组件选项中:

directives: {
  forCallback(el, binding) {
      let element = binding.value
      if (element.key == element.array.length - 1)
      if (typeof element.callback === 'function') {
        element.callback()
      }
    }
}

v-for-callback需要一个选项对象。

{
  key: key, // this will contain the item key that was generated with `v-for`
  array: items, // the actual `v-for` array
  callback: callback // your callback function (in this example, it's defined in the component methods
}

然后在您的组件选项中:

methods: {
    callback() {
        console.log('v-for loop finished')          
    }
}

答案 2 :(得分:0)

你可以在循环中使用if,如下所示:

<template>
    <div class="row">
        <div class="col-md-3" v-for="(item, index in items)">
            <div v-if="index === items.length -1">
                Display what ever you want here 
            <\div>
        </div>
    </div>
</template>

或者如果你想在js中做某事,那么在相同的条件成立时调用一个函数,如下所示:

 <div class="col-md-3" v-for="(item, index in items)">
            <div v-if="index === items.length -1 && lastIteration()">
                Display what ever you want here 
            <\div>
        </div>

methods: {
  lastIteration() {
     alert("last");
}
}

我没有测试它,但是ideea很好,它应该工作。希望回答你的问题:)