简体   繁体   English

如何使用VueJS中的计算属性从v-for指令过滤器中删除重复项?

[英]How do I remove repetition from a v-for directive filter using computed properties in VueJS?

I have a list of clubs using a v-for loop as well as a dropdown select. 我有一个使用v-for循环以及下拉选择的俱乐部列表。 This works well except that the options in this select tag get repeated. 除重复选择此select标记中的选项外,此方法效果很好。 To remove these I am using a computed property. 为了删除这些,我使用了计算属性。 However, when I add the computed property to my filtered list or my js file the content no longer renders. 但是,当我将计算属性添加到我的过滤列表或js文件中时,内容不再呈现。 This code is originally from a tutorial, but somehow doesn't work for me when I implement the computed property. 这段代码最初来自于一个教程,但是当我实现计算属性时,某种程度上对我不起作用。 Any help welcome. 任何帮助欢迎。

const clubs = [
    {
   name: 'Tigers',
   location: 'Manchester',
   members: '22',
   registered: 'No',
   pitch: 'Grass'
    },
    {
   name: 'Dolphins',
   location: 'Miami',
   members: '19',
   registered: 'Yes',
   pitch: 'Grass'
    },
    {
   name: 'Bleu Sox',
   location: 'Paris',
   members: '13',
   registered: 'Yes',
   pitch: 'Astroturf'   
    } 
];

const app = new Vue({
  el: '#app',
  data: {
    club: 'clubs',
    pitch: ''
  },
  methods: {
    toggleDetails: function(club) {
      this.$set(club, 'showDetails', !club.showDetails)
    },
    filterList: function(event) {
      this.pitch = event.target.value;
    }, 
    computed: {
      // REMOVES DUPLICATION
      uniqueItemsList: function() {
        const types = [];
        this.club.forEach((club)=> {
          if(!types.includes(club.pitch)){
            types.push(club.pitch);
          }
        });
        return types;
      }
    }
  }
});

My HTML looks as follows: 我的HTML如下所示:

<div id="app">

<select v-on:change="filterList">
   <option>Select a surface</option>
   <option v-for="club in uniqueItemsList()">{{club.pitch}}</option>
</select>

  <ul>
    <li v-show="pitch === club.pitch" v-for="club in clubs" v-on:click="toggleDetails(club)">
      <h1>{{club.name}}</h1>
      <div v-show="club.showDetails">
          <p>{{club.location}}</p>
          <p>{{club.members}}</p>
      </div>
    </li>
  </ul>

</div>

You have placed your computed in methods ( computed should be outside the methods ). 你已经把你computedmethodscomputed应该是外面methods )。 Plus, you have an error in this.club.forEach , should be clubs (global variable). 另外,您在this.club.forEach有错误,应该是clubs (全局变量)。

I have fixed your Vue.js-code-issues and here is a working copy: 我已经修复了您的Vue.js代码问题,这是一个工作副本:

    const clubs = [
    {
   name: 'Tigers',
   location: 'Manchester',
   members: '22',
   registered: 'No',
   pitch: 'Grass'
    },
    {
   name: 'Dolphins',
   location: 'Miami',
   members: '19',
   registered: 'Yes',
   pitch: 'Grass'
    },
    {
   name: 'Bleu Sox',
   location: 'Paris',
   members: '13',
   registered: 'Yes',
   pitch: 'Astroturf'   
    } 
];

const app = new Vue({
  el: '#app',
  data: {
    club: 'clubs',
    pitch: 'clubs'
  },
  methods: {
    uniqueItemsList: function() {
        const types = [];
        clubs.forEach((club)=> {
          if(!types.includes(club.pitch)){
            types.push(club.pitch);
          }
        });
        return types;
      },
    toggleDetails: function(club) {
      this.$set(club, 'showDetails', !club.showDetails)
    },
    filterList: function(event) {
      this.pitch = event.target.value;
    }, 
  },
});

And HTML part: 和HTML部分:

<div id="app">

<select v-on:change="filterList">
   <option>Select a surface</option>
   <option v-for="club in uniqueItemsList()">{{club}}</option>
</select>

  <ul>
    <li v-show="pitch === club.pitch" v-for="club in clubs" v-on:click="toggleDetails(club)">
      <h1>{{club.name}}</h1>
      <div v-show="club.showDetails">
          <p>{{club.location}}</p>
          <p>{{club.members}}</p>
      </div>
    </li>
  </ul>

</div>

Output result: 输出结果:

在此处输入图片说明

To computed property works correctly, it must use data variables (or other computed variables), because internally vuejs will watch these variables with $watch fn. 为了使计算属性正确运行,它必须使用data变量(或其他computed变量),因为在内部vuejs将使用$watch fn监视这些变量。 So just put clubs on data that computed will watch for changes in this array. 因此,只需将球棒放在计算出的数据上,即可观察该数组的变化。

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

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