如何在Vuetify中将内容对齐到v-card组件的中心?

8ehkhllq  于 2023-06-06  发布在  Vue.js
关注(0)|答案(2)|浏览(231)

目前在products.vue中,我有一个包含4个对象的productList数组。我将遍历数组并将每个对象传递给ProductsItem.vue组件。在这个组件中,我使用vuetify创建卡片。
我无法将内容对齐卡的中心。
下面是我的代码,我的卡的截图和期望的结果
Products.vue

<template>
      <div>
        <h1>Products</h1>
        <v-container class="my-5">
          <v-layout row wrap>
            <v-flex xs12 sm6 md4 v-for="productItem in productList" 
    :key="productItem.id">
              <ProductItems :productItem="productItem"/>
            </v-flex>
          </v-layout>
        </v-container>
      </div>
    </template>

    <script>
    import ProductItems from "@/components/ProductItems";

    export default {
      data() {
        return {
          productList: [
            {
              id: 1,
              name: "Superdry",
              description: "Rookie Aviator Patched Bomber"
            },
            {
              id: 2,
              name: "SuperHot",
              description: "Rookie Aviator Patched Bomber"
            },
            {
              id: 3,
              name: "Buron MensWear",
              description: "Skinny Fit Oxford Shirt In White"
            },
            {
              id: 4,
              name: "Asos",
              description: "slim shirt with stretch in blue"
            }
          ]
        };
      },

        components: {
          ProductItems
        }
      }
    </script>

ProductItem.vue

<template>
      <v-card flat class="ma-3 text-xs-center">
      <v-img src="https://cdn.vuetifyjs.com/images/cards/desert.jpg" aspect- 
    ratio="2.75"></v-img>
        <v-card-title primary-title>
          <div>
            <h3 class="headline pink--text text--accent-2"> 
    {{productItem.name}}</h3>
            <div>{{productItem.description}}</div>
          </div>
        </v-card-title>
        <v-card-actions>
          <v-btn flat color="orange">Add to Cart</v-btn>
        </v-card-actions>
      </v-card>
    </template>

    <script>
    export default {
      props: ["productItem"],
      data() {
        return {};
      }
    };
    </script>

<style>
</style>

gcmastyq

gcmastyq1#

更新:适用于Vuetify 1.5和2版本:

要集中v-card-text内容,只需应用class=text-center
v-card-titlev-card-actionsflex组件,因此通过将class="justify-center"添加到两者中,您可以集中整个事情:

<v-card-title primary-title class="justify-center">
  <div>
    <h3 class="headline pink--text text--accent-2">Superdry</h3>
    <div>Rookie Aviator Patched BomberproductItem.description</div>
  </div>
</v-card-title>
<v-card-actions class="justify-center">
  <v-btn flat color="orange">Add to Cart</v-btn>
</v-card-actions>
avkwfej4

avkwfej42#

您还可以使用v-spacerv-card的****v-card-titlev-card-text**中的组件居中

适用于1.5.x和2.x

<v-card-title>
  <v-spacer />
  <div class="text-center">
    <h3 class="headline pink--text text--accent-2">Headline</h3>
    <div>Some description about the headline</div>
  </div>
  <v-spacer />
</v-card-title>
<v-card-actions>
  <v-spacer />
  <v-btn color="orange">Some Button</v-btn>
  <v-spacer />
</v-card-actions>

这里有一些沙箱用于快速演示

适用于1.5.xhttps://codesandbox.io/s/vuetify-1-5-v-spacer-f7s0ek?file=/src/App.vue
适用于2.xhttps://codesandbox.io/s/v-spacer-for-vuetify-2-x-bsi9x8?file=/src/App.vue

相关问题