简体   繁体   English

vue.js 如何在 for 循环中调用函数并传递当前项?

[英]vue.js How to call a function inside a for loop and pass the current item?

Using vue.js 2, inside a for loop, I need to render a row only if the current iterated item passes some test.使用 vue.js 2,在 for 循环中,仅当当前迭代项通过某些测试时,我才需要呈现一行。

The test is complex so a simple v-if="item.value == x" wont do.测试很复杂,所以简单的v-if="item.value == x"不会做。

I've written a function named testIssue that accepts the item and returns true or false and tried to use that is a v-if like this:我编写了一个名为testIssue的函数,它接受项目并返回 true 或 false 并尝试使用它是这样的v-if

<template v-for="issue in search_results.issues">
  <tr v-if="testIssue(issue)">
     ....
  </tr>
</template>


var releaseApp = new Vue({
  el: '#release-app',
  methods: {
    testIssue: function(issue) {
      console.log(issue);
      console.log('test');
    },
  },
  mounted: function() {},
  data: {
    search_results: {
      issues: []
    },
  }
});

However, testIssue is never called.但是,永远不会调用testIssue

If I change the line to <tr v-if="testIssue"> , the function is called but then I dont have the issue variable that I need to test.如果我将行更改为<tr v-if="testIssue"> ,则会调用该函数,但是我没有需要测试的issue变量。

I also tried <tr v-if="releaseApp.testIssue(issue)">我也试过<tr v-if="releaseApp.testIssue(issue)">

How can I call a function in a v-if declaration inside a for loop and pass the current item?如何在 for 循环内的v-if声明中调用函数并传递当前项?

First of all you can't do v-for on a <template> tag as you can have only one element per template.首先,您不能对<template>标记执行v-for ,因为每个模板只能有一个元素。

You can add a v-if on the same element as v-for, and it just won't render the element that doesn't pass the v-if.您可以在与 v-for 相同的元素上添加 v-if,它只是不会渲染未通过 v-if 的元素。 If it's a spearate component per row then it's better to do v-for in parent component and pass the issue by props to child component.如果它是每行的独立组件,那么最好在父组件中执行 v-for 并通过道具将问题传递给子组件。

Parent:家长:

<child-comp v-for="issue in search_results.issues" v-if="testIssue(issue)">
</child-comp>

Child:孩子:

<template>
  <tr>
    {{issue}}
  </tr>
</template>

Your scenario works with a similar example in this fiddle .您的场景适用于此小提琴中的类似示例。

But you can try it this way also:但是你也可以这样尝试:

You can create a custom directive named v-hide and pass issue to it as its value.您可以创建一个名为v-hide自定义指令并将issue作为其值传递给它。

Then in the directive you can test for testIssue() and set the particular element's display to none然后在指令中您可以测试testIssue()并将特定元素的显示设置为无

<template v-for="issue in search_results.issues">
  <tr v-hide="issue">
     ....
  </tr>
</template>


var releaseApp = new Vue({
  el: '#release-app',
  directive:{
    hide: {
        bind(el, binding, Vnode){
            var vm = Vnode.context;
            var issue = binding.value;
            if(vm.testIssue(issue)){
                el.style.display = 'none';
            }
        }
    }
  },
  methods: {
    testIssue: function(issue) {
      console.log(issue);
      console.log('test');
    },
  },
  mounted: function() {},
  data: {
    search_results: {
      issues: []
    },
  }
});
 

You can also try creating a computed item that makes use of the filter method so that you have an array where all elements pass the test function before actually rendering (in this case, just returning odd numbers):您还可以尝试创建一个使用filter方法的计算项,以便您拥有一个数组,其中所有元素在实际渲染之前通过测试函数(在这种情况下,只返回奇数):

https://codepen.io/aprouja1/pen/BZxejL https://codepen.io/aprouja1/pen/BZxejL

computed:{
    compIssues(){
      return this.search_results.issues.filter(el => el%2===1)
    }
  },

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM