【问题标题】:How to display all the products when clicking on "selectAll" and also before selecting any filter?单击“selectAll”时以及在选择任何过滤器之前如何显示所有产品?
【发布时间】:2020-12-07 14:29:42
【问题描述】:

**我正在使用 Vue 和 Firebase 创建一个网上商店。过滤器有效,但我希望它在选择特定过滤器之前显示所有产品。这是过滤器的链接,另外,我有一个显示所有产品的链接:**

<v-list-item link>
  <v-list-item-content @click="selectAll()">
    <v-list-item-title>All Categories</v-list-item-title>
  </v-list-item>
<v-list-item link>
  <v-list-item-content @click="selectElectronics()">
    <v-list-item-title>Electronics & Lights</v-list-item-title>
  </v-list-item-content>
 </v-list-item>
 <v-list-item link>
    <v-list-item-content @click="selectKitchen()">
      <v-list-item-title>Kitchen</v-list-item-title>
    </v-list-item-content>
 </v-list-item>
 <v-list-item link>
   <v-list-item-content @click="selectHobbies()">
      <v-list-item-title>Hobbies & Free-time</v-list-item-title>
   </v-list-item-content>
 </v-list-item>

<v-col
             sm="5"
             md="6"
             v-for="item in filterCategoryItems"
             :key="item.name"
           >

**在计算中应该改变什么以使其工作?它以前与注释行一起工作,但现在不行了,我不知道为什么 渲染错误:“TypeError:无法读取未定义的属性‘包含’” **

   export default {
 data() {
return {
categoryChoice: "",
};
 },
 methods: {
   selectAll() {
     this.categoryChoice = "";
   },
   selectElectronics() {
     this.categoryChoice = "electronics";
   },
   selectKitchen() {
     this.categoryChoice = "kitchen";
   },
   selectHobbies() {
     this.categoryChoice = "hobbies";
   },

computed: {
   filterCategoryItems() {
     return this.menuItems.filter(
       filterItem => filterItem.category == this.categoryChoice 
     //    (filterItem) => filterItem.category.includes(this.categoryChoice) 
     );
 },
 menuItems() {
     return this.$store.getters.getMenuItems;
   },
   basket() {
     return this.$store.getters.getBasketItems;
   },
   favItems() {
     return this.$store.getters.getFavItems;
   },
 },
};

【问题讨论】:

    标签: vue.js


    【解决方案1】:

    您的脚本未正确构建。如果你修复了缩进,你可能已经注意到了。 data() 也应该返回一个对象:

    <script>
    export default {
      data() {
        return {
          categoryChoice: ""
        };
      },
      methods: {
        selectAll() {
          this.categoryChoice = "";
        },
        selectElectronics() {
          this.categoryChoice = "electronics";
        },
        selectKitchen() {
          this.categoryChoice = "kitchen";
        },
        selectHobbies() {
          this.categoryChoice = "hobbies";
        }
      },
      computed: {
        filterCategoryItems() {
          return this.menuItems.filter(
            (filterItem) => filterItem.category == this.categoryChoice
            //    (filterItem) => filterItem.category.includes(this.categoryChoice)
          );
        },
        menuItems() {
          return this.$store.getters.getMenuItems;
        },
        basket() {
          return this.$store.getters.getBasketItems;
        },
        favItems() {
          return this.$store.getters.getFavItems;
        }
      }
    };
    </script>
    

    【讨论】:

    • 感谢您的回答。我的代码中确实有 data() 返回,我只是忘了在这里输入
    猜你喜欢
    • 1970-01-01
    • 2019-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-14
    • 2018-01-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多