如何使用 Vuetify 网格在 v-for 循环中显示商店项目?

JS_*_*e18 0 javascript firebase vue.js vuex vuetify.js

我正在尝试使用 Vuetify 网格系统在 v-for 循环中显示卡片项目。该循环被设置为遍历从 Vuex 存储文件(“item in this.$store.getters.getItems”)返回到模板的动态输入 Firestore 项目,然后将其呈现为 Vuetify 卡片。我成功地设置了循环以在容器内的小卡片中呈现项目。但是,我希望这些卡片在网格中呈现。换句话说,我想创建一个断点,以便在 3 张牌之后,例如,第 4、5 和 6 张牌下降到新的行。我怎样才能做到这一点?我了解如何在更简单的设置中执行此操作,而无需在 v-for 循环中使用 Vuex getter 方法。但是当 Vuex 方法开始进入图片时,这是如何工作的呢?我的代码如下:

主页.vue

<template>
 <div id="home">
   <v-container>
     <v-text-field v-model="myTodo" placeholder="add input"></v-text-field>
     <v-btn @click="addToDo">Add</v-btn>
   </v-container>

  <v-container>
    <v-flex md7>
      <v-card class="elevation-0 transparent card-container grey">
        <v-card-title primary-title class="layout justify-center">
          <div class="headline text-xs-center">CARD CONTAINER</div>
        </v-card-title>
        <v-flex d-flex>
          <v-card class="card-container" v-for="item in this.$store.getters.getItems" :key="item.id">
            {{ item.title }}<v-btn @click="deleteItem(item.id)">Delete</v-btn>
          </v-card>
        </v-flex>
      </v-card>
    </v-flex>
  </v-container>
 </div>
</template>

<script>
import { db } from '@/main'

export default {
  name: 'home',
  beforeCreate: function () {
    this.$store.dispatch('setItems')
  },
  data: function () {
    return {
      myTodo: '',
      errors: ''
    }
  },
  methods: {
    addToDo: function () {
      this.errors = ''
      if (this.myTodo !== '') {
        db.collection('items').add({
          title: this.myTodo,
          created_at: Date.now()
        }).then((response) => {
          if (response) {
            this.myTodo = ''
          }
        }).catch((error) => {
          this.errors = error
        })
      } else {
        this.errors = 'Please enter some text'
      }
    },
    deleteItem: function (id) {
      if (id) {
        db.collection("items").doc(id).delete().then(function() {
          console.log('Document successfully deleted')
        }).catch(function(error) {
          this.error = error
        })
      } else {
        this.error = 'Invalid ID'
      }
    }
  }
}
</script>

<style>
  .card-container {
    margin: 10px;
    padding: 10px;
  }
</style>

Run Code Online (Sandbox Code Playgroud)

商店.js

import Vue from 'vue'
import Vuex from 'vuex'
import { db } from '@/main'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    items: null
  },
  getters: {
    getItems: state => {
      return state.items
    }
  },
  mutations: {
    setItems: state => {
      let items = []
      db.collection('items').orderBy('created_at').onSnapshot((snapshot) => {
        items = []
        snapshot.forEach((doc) => {
          items.push({ id: doc.id, title: doc.data().title })
        })
        state.items = items
      })
    }
  },
  actions: {
    setItems: context => {
      context.commit('setItems')
    }
  }
})

Run Code Online (Sandbox Code Playgroud)

Mar*_*ini 9

实际上,您只是创建了一个卡片列表,它们将在v-flex没有任何进一步指令的情况下显示在 a 中。

要拥有网格布局,您应该使用v-layout加号v-flex.

<v-flex d-flex>
   <v-layout wrap>
       <v-flex md4 v-for="item in this.$store.getters.getItems" :key="item.id">
           <v-card class="card-container">
            {{ item.title }}<v-btn @click="deleteItem(item.id)">Delete</v-btn>
          </v-card>
       </v-flex>
   </v-layout>
</v-flex>
Run Code Online (Sandbox Code Playgroud)

在这段代码中,我用v-layout带有wrap属性的a 包装卡片,不需要v-layout为该行写一个新的。

for 循环移到了v-flex,我给单元格的大小为 4。

在网格布局中,您有 12 个框,如果您需要 3 个,则每个框的大小必须为 4 (md4)。

如果你需要一个更灵活的布局,你应该把它v-layout放在循环里面,每次你想要一个新行时都打印一个新的。

笔记

我是 vuetify 的新手,所以不确定是否有更好的方法来实现这一目标。