【发布时间】:2022-01-03 15:28:53
【问题描述】:
我有以下一组按钮:
<template>
<v-card
color="secondary"
elevation="6"
>
<v-card-title>
<v-spacer></v-spacer>
Three buttons
</v-card-title>
<v-divider></v-divider>
<v-card-text>
<v-row>
<v-col cols="12" sm="4" v-for="button in buttons" :key="button.id">
<v-btn color="primary" x-large block>
<span style="word-break: normal !important;">{{button.text}}</span>
</v-btn>
</v-col>
</v-row>
</v-card-text>
</v-card>
</template>
<script>
export default {
data () {
return {
//This is not a production code. The button texts are obtained via api and their length is variable
buttons: [
{id:1, text: "I'm a button"},
{id:2, text: "I'm another button. Bla, bla, bla, bla"},
{id:3, text: "I'm a button too"}
]
}
},
}
</script>
在此示例中,文本是硬编码的,但实际上我是从 API 获取文本。它的长度是可变的,我事先无法知道。我需要:
- 文本永远不会溢出按钮的空间,因为它发生在按钮 2 中。
- 让按钮文本使用它需要的行。 1,2,10 ...取决于它的长度。 始终垂直增长并带有换行符,这将授予 css 属性'word-break: normal;'
- 按钮的宽度保持固定,占据列的整个宽度,因为 'block' 属性应该会导致。
所有这些都必须在桌面、平板电脑和移动设备视图中实现。
你可以测试一下here。
【问题讨论】:
-
也许这可能是帖子stackoverflow.com/questions/46639068/…的解决方案
-
不,我不想只显示部分文本。我想显示所有文本,占据它需要的行
-
那么你可以有一个知道宽度的函数并将它用作变量来知道你应该在哪里换行。
-
这对我来说似乎不是一个干净的解决方案。我宁愿使用 Vuetify 库或 CSS 中的内置功能,就像 word-break 属性一样。
标签: css vue.js vuetify.js