vue.js 如何将按钮与v-card标题放在同一行?

brccelvz  于 2023-04-07  发布在  Vue.js
关注(0)|答案(3)|浏览(284)

我有一个标题,子标题,图标和按钮在我的v-card。我的目标是把我的创建按钮的权利,但我似乎不能做到这一点。

<template>
    <v-row class="pl-5">
        <v-card class="pl-5" elevation="0">
            <v-card-title>
                <v-icon left x-large color="primary">{{ icon }}</v-icon>
                <span class="font-weight-bold">{{ title }}</span>
            </v-card-title>

            <v-card-subtitle> Subheader </v-card-subtitle>

            <v-spacer></v-spacer>
            <router-link v-if="type == 'index'" :to="`/${name.toLowerCase().replace(' ', '-')}/create`">
                <v-btn outlined class="blue--text mt-5 mr-8">
                    <span>Create</span>
                </v-btn>
            </router-link>
        </v-card>
    </v-row>
</template>
<script>
export default {
    props: {
        icon: String,
        name: String,
        title: String,
        subtitle: String,
        type: String
    },
    components: {}
}
</script>
<style scoped></style>

如果我在<v-card-subtitle>中移动<router-link>
我明白了

如果我在<v-card-title>中移动<router-link>
我明白了

有人能给予我一下吗?
小提琴https://jsfiddle.net/bheng/h2u870dv/
如果我这样做:

<v-row class="pl-5">
    <v-card-title>
    <span class="material-icons">
        favorite_border
    </span>
    <p class="font-weight-bold">TITLE</p>
    <p class="caption">ST</p>
    </v-card-title>
    <v-spacer></v-spacer>
    <v-btn>Create</v-btn>
</v-row>

我明白


按钮似乎在我想要的位置,但标题和副标题错位非常严重。我现在卡住了。

kcugc4gi

kcugc4gi1#

您可以在v-row中添加div或v-col,并使用css以您想要的方式对齐项目:

new Vue({
  el: '#app',
  vuetify: new Vuetify(),
  data() {
    return {
      icon: 'favorite_border',
      name: 'link',
      title: 'TITLE',
      subtitle: 'https://fonts.google.com/icons?selected=Material+Icons',
      type: 'index'
    }
  }
})
.myclass {
  width: 100%;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: flex-start;
}
<link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/@mdi/font@6.x/css/materialdesignicons.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.min.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Material+Icons" rel="stylesheet">
<div id="app">
  <v-app>
    <v-main>
      <v-container>
        <v-row class="pl-5">
          <div class="myclass">
            <v-card class="pl-5 mycard" elevation="0">
              <v-card-title>
                <v-icon left x-large color="primary">{{ icon }}</v-icon>
                <span class="font-weight-bold">{{ title }}</span>
              </v-card-title>
              <v-card-subtitle> Subheader </v-card-subtitle>
              <v-spacer></v-spacer>
            </v-card>
            <a v-if="type == 'index'" :href="`/${name.toLowerCase().replace(' ', '-')}/create`">
              <v-btn outlined class="blue--text mt-5 mr-8">
                <span>Create</span>
              </v-btn>
            </a>
          </div>
        </v-row>
      </v-container>
    </v-main>
  </v-app>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2.x/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.js"></script>
whhtz7ly

whhtz7ly2#

你的问题是你把你的按钮放在你声明的v-card里面来设置标题的样式。而这张卡片只是一个正方形的盒子,因为你没有指定任何宽度。如果你设置elevation=“1”,你可以清楚地看到这一点。
只要把你的按钮放在v-card外面,v-spacer就可以工作了。或者,你也可以把v-card的宽度设置为100%。顺便说一下,你也可以在你的v-btn中使用to prop ,所以没有必要有一个v-router-link
检查我做的这个codesandbox:https://codesandbox.io/s/stack-71444864-v-spacer-example-0tx24n?file=/src/components/Example.vue

<v-row class="pl-5">
   <v-card class="pl-5" elevation="0">
      <v-card-title>
         <v-icon left x-large color="primary"> mdi-home </v-icon>
         <span class="font-weight-bold">Testing</span>
      </v-card-title>
      <v-card-subtitle class="pb-0"> Subheader </v-card-subtitle>
   </v-card>
   <v-spacer></v-spacer>
   <v-btn to="/about" outlined class="blue--text mt-5 mr-8">
      <span>Create</span>
   </v-btn>
</v-row>

更新:v-cols和带断点条件的自定义CSS

我看到你有兴趣学习如何使用vuetify's grid进行设计。使用v-cols和断点条件的好处是,你可以在不同的视口中更好地控制你的设计。就像在移动的视图中一样。这就是我的做法。
首先,我将网格分成2列,一列用于标题,一列用于按钮,在cols视图(以前称为xs(超小))中,我将它们的大小设置为12列,在sm(小)和更高的视图中,我将它们的大小设置为6列。
标题栏没有变化,在底部栏,我使用类d-flex justify-end将按钮全部向右移动,align-center将按钮垂直居中在v-row的中间。
然后,我使用断点条件来激活按钮的block属性,并根据当前视口对整个v行应用填充。

<v-row :class="$vuetify.breakpoint.smAndUp ? 'px-4' : ''">
   <v-col cols="12" sm="6">
      <v-card elevation="0">
         <v-card-title>
            <v-icon left x-large color="primary"> mdi-home </v-icon>
            <span class="font-weight-bold">Testing</span>
         </v-card-title>
         <v-card-subtitle class="pb-0"> Subheader </v-card-subtitle>
      </v-card>
   </v-col>
   <v-col cols="12" sm="6" class="d-flex justify-end align-center">
      <v-btn to="/about" :block="$vuetify.breakpoint.xsOnly ? true : false" outlined class="blue--text">
         <span>Create</span>
      </v-btn>
   </v-col>
</v-row>

0vvn1miw

0vvn1miw3#

你正在使用Vuetify的权利?然后更容易,只需添加按钮在v-card-title标签与绝对右上角attrs.然后v-btn也允许:到(https://v2.vuetifyjs.com/en/api/v-btn/#props)

<v-card-title>
    <v-icon left x-large color="primary">{{ icon }}</v-icon>
    <span class="font-weight-bold">{{ title }}</span>
    <v-btn outlined absolute right top v-if="type === 'index'" class="blue--text mt-5 mr-8" :to="name.toLowerCase().replace(' ', '-') + '/create'">
    </v-btn>
</v-card-title>

相关问题