多行文本框

多行文本框组件用于收集大量文本数据。

用例

v-textarea 最简单的形式是多行文本字段,可用于大量文本。

template


  1. <template>
  2. <v-container fluid>
  3. <v-row>
  4. <v-col cols="12" md="6">
  5. <v-textarea
  6. name="input-7-1"
  7. label="Default style"
  8. value="The Woodman set to work at once, and so sharp was his axe that the tree was soon chopped nearly through."
  9. hint="Hint text"
  10. ></v-textarea>
  11. </v-col>
  12. <v-col cols="12" md="6">
  13. <v-textarea
  14. solo
  15. name="input-7-4"
  16. label="Solo textarea"
  17. ></v-textarea>
  18. </v-col>
  19. <v-col cols="12" md="6">
  20. <v-textarea
  21. filled
  22. name="input-7-4"
  23. label="Filled textarea"
  24. value="The Woodman set to work at once, and so sharp was his axe that the tree was soon chopped nearly through."
  25. ></v-textarea>
  26. </v-col>
  27. <v-col cols="12" md="6">
  28. <v-textarea
  29. outlined
  30. name="input-7-4"
  31. label="Outlined textarea"
  32. value="The Woodman set to work at once, and so sharp was his axe that the tree was soon chopped nearly through."
  33. ></v-textarea>
  34. </v-col>
  35. </v-row>
  36. </v-container>
  37. </template>

Textareas(多行文本框) - 图1

API

从下面选择您想要的组件,并查看可用的属性、插槽、事件和函数。

Textareas(多行文本框) - 图2

实战场

template script


  1. <template>
  2. <v-container fluid>
  3. <v-row>
  4. <v-col
  5. sm="6"
  6. cols="12"
  7. >
  8. <v-text-field
  9. v-model="label"
  10. label="Label"
  11. ></v-text-field>
  12. <v-text-field
  13. v-model="hint"
  14. label="Hint"
  15. ></v-text-field>
  16. <v-text-field
  17. v-model="placeholder"
  18. label="Placeholder"
  19. ></v-text-field>
  20. </v-col>
  21. <v-col cols="12" sm="6">
  22. <v-text-field
  23. v-model="rowHeight"
  24. :disabled="!autoGrow"
  25. class="mx-4"
  26. label="Row height - px"
  27. max="500"
  28. min="1"
  29. step="1"
  30. style="width: 125px"
  31. type="number"
  32. ></v-text-field>
  33. <v-text-field
  34. v-model="rows"
  35. class="mx-4"
  36. label="Rows"
  37. max="50"
  38. min="1"
  39. step="1"
  40. style="width: 125px"
  41. type="number"
  42. ></v-text-field>
  43. </v-col>
  44. <v-col cols="12"></v-col>
  45. <v-col cols="12" md="6">
  46. <v-switch
  47. v-model="autoGrow"
  48. class="ma-1"
  49. label="Auto-grow"
  50. ></v-switch>
  51. <v-switch
  52. v-model="clearable"
  53. class="ma-1"
  54. label="Clearable"
  55. ></v-switch>
  56. <v-switch
  57. v-model="filled"
  58. class="ma-1"
  59. label="Filled"
  60. ></v-switch>
  61. <v-switch
  62. v-model="flat"
  63. class="ma-1"
  64. label="Flat (requires Solo)"
  65. :disabled="!solo"
  66. ></v-switch>
  67. <v-switch
  68. v-model="loading"
  69. class="ma-1"
  70. label="Loading"
  71. ></v-switch>
  72. <v-switch
  73. v-model="outlined"
  74. class="ma-1"
  75. label="Outlined"
  76. ></v-switch>
  77. <v-switch
  78. v-model="persistentHint"
  79. class="ma-1"
  80. label="Persistent Hint"
  81. ></v-switch>
  82. <v-switch
  83. v-model="rounded"
  84. class="ma-1"
  85. label="Rounded (requires Filled or Outlined)"
  86. ></v-switch>
  87. <v-switch
  88. v-model="shaped"
  89. class="ma-1"
  90. label="Shaped (requires Filled or Outlined)"
  91. ></v-switch>
  92. <v-switch
  93. v-model="singleLine"
  94. class="ma-1"
  95. label="Single-line"
  96. ></v-switch>
  97. <v-switch
  98. v-model="solo"
  99. class="ma-1"
  100. label="Solo"
  101. ></v-switch>
  102. </v-col>
  103. <v-col
  104. cols="12"
  105. md="6"
  106. >
  107. <v-sheet
  108. elevation="12"
  109. class="pa-12"
  110. >
  111. <v-textarea
  112. v-model="model"
  113. :auto-grow="autoGrow"
  114. :clearable="clearable"
  115. :counter="counter ? counter : false"
  116. :filled="filled"
  117. :flat="flat"
  118. :hint="hint"
  119. :label="label"
  120. :loading="loading"
  121. :no-resize="noResize"
  122. :outlined="outlined"
  123. :persistent-hint="persistentHint"
  124. :placeholder="placeholder"
  125. :rounded="rounded"
  126. :row-height="rowHeight"
  127. :rows="rows"
  128. :shaped="shaped"
  129. :single-line="singleLine"
  130. :solo="solo"
  131. ></v-textarea>
  132. <div class="mt-12 text-center">
  133. Value: {{ model }}
  134. </div>
  135. </v-sheet>
  136. </v-col>
  137. </v-row>
  138. </v-container>
  139. </template>
  1. <script>
  2. export default {
  3. data: () => ({
  4. autoGrow: false,
  5. autofocus: true,
  6. clearable: false,
  7. counter: 0,
  8. filled: false,
  9. flat: false,
  10. hint: '',
  11. label: '',
  12. loading: false,
  13. model: 'I\'m a textarea.',
  14. noResize: false,
  15. outlined: false,
  16. persistentHint: false,
  17. placeholder: '',
  18. rounded: false,
  19. rowHeight: 24,
  20. rows: 1,
  21. shaped: false,
  22. singleLine: false,
  23. solo: false,
  24. }),
  25. }
  26. </script>

Textareas(多行文本框) - 图3

示例

下面是一些简单到复杂的例子。

图标

append-iconprepend-icon 属性有助于将上下文添加到 v-textarea

template


  1. <template>
  2. <v-container>
  3. <v-row>
  4. <v-col cols="12" sm="6">
  5. <v-textarea
  6. class="mx-2"
  7. label="prepend-icon"
  8. rows="1"
  9. prepend-icon="comment"
  10. ></v-textarea>
  11. </v-col>
  12. <v-col cols="12" sm="6">
  13. <v-textarea
  14. append-icon="comment"
  15. class="mx-2"
  16. label="append-icon"
  17. rows="1"
  18. ></v-textarea>
  19. </v-col>
  20. <v-col cols="12" sm="6">
  21. <v-textarea
  22. prepend-inner-icon="comment"
  23. class="mx-2"
  24. label="prepend-inner-icon"
  25. rows="1"
  26. ></v-textarea>
  27. </v-col>
  28. <v-col cols="12" sm="6">
  29. <v-textarea
  30. append-outer-icon="comment"
  31. class="mx-2"
  32. label="append-outer-icon"
  33. rows="1"
  34. ></v-textarea>
  35. </v-col>
  36. </v-row>
  37. </v-container>
  38. </template>

Textareas(多行文本框) - 图4

自动增长

当使用 auto-grow 属性时,当包含的文本超过其大小时,多行文本框的大小将自动增加。

template


  1. <template>
  2. <v-container fluid>
  3. <v-textarea
  4. name="input-7-1"
  5. filled
  6. label="Label"
  7. auto-grow
  8. value="The Woodman set to work at once, and so sharp was his axe that the tree was soon chopped nearly through."
  9. ></v-textarea>
  10. </v-container>
  11. </template>

Textareas(多行文本框) - 图5

背景颜色

background-colorcolor 属性让您更好地控制样式 v-textarea 的样式。

template


  1. <template>
  2. <v-container>
  3. <v-textarea
  4. background-color="light-blue"
  5. color="black"
  6. label="Label"
  7. ></v-textarea>
  8. <v-textarea
  9. background-color="grey lighten-2"
  10. color="cyan"
  11. label="Label"
  12. ></v-textarea>
  13. <v-textarea
  14. background-color="amber lighten-4"
  15. color="orange orange-darken-4"
  16. label="Label"
  17. ></v-textarea>
  18. </v-container>
  19. </template>

Textareas(多行文本框) - 图6

浏览器自动完成

autocomplete 属性为您提供使浏览器能够预测用户输入的选项。

template


  1. <template>
  2. <v-container fluid>
  3. <v-textarea
  4. autocomplete="email"
  5. label="Email"
  6. ></v-textarea>
  7. </v-container>
  8. </template>

Textareas(多行文本框) - 图7

可清除

您可以使用 clearable 属性清除 v-textarea 中的文本,并自定义与 clearable-icon 属性一起使用的图标。

template


  1. <template>
  2. <v-container fluid>
  3. <v-textarea
  4. clearable
  5. clear-icon="cancel"
  6. label="Text"
  7. value="This is clearable text."
  8. ></v-textarea>
  9. </v-container>
  10. </template>

Textareas(多行文本框) - 图8

Alpha Theme

Complete theme experience including enhanced Vue CLI, full documentation, 5 custom components and much more!

ads by Vuetify

](https://store.vuetifyjs.com/product/alpha-theme?ref=vuetifyjs.com)

计数器

counter 属性通知用户 v-textarea 的字符数限制。

template script


  1. <template>
  2. <v-container fluid>
  3. <v-textarea
  4. counter
  5. label="Text"
  6. :rules="rules"
  7. :value="value"
  8. ></v-textarea>
  9. </v-container>
  10. </template>
  1. <script>
  2. export default {
  3. data: () => ({
  4. rules: [v => v.length <= 25 || 'Max 25 characters'],
  5. value: 'Hello!',
  6. }),
  7. }
  8. </script>

Textareas(多行文本框) - 图9

没有调整大小

v-textarea 使用 no-resize 属性,可以选择保持大小不变​​,而不管其内容的大小如何。

template script


  1. <template>
  2. <v-container fluid>
  3. <v-textarea
  4. label="Text"
  5. no-resize
  6. rows="1"
  7. :value="value"
  8. ></v-textarea>
  9. </v-container>
  10. </template>
  1. <script>
  2. export default {
  3. data: () => ({
  4. value: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.',
  5. }),
  6. }
  7. </script>

Textareas(多行文本框) - 图10

rows 属性可让您定义文本区域有多少行,当与 row-height 属性结合使用时,您可以通过定义行的高度进一步自定义行。

template


  1. <template>
  2. <v-container fluid>
  3. <v-row>
  4. <v-col cols="12" sm="6">
  5. <v-textarea
  6. label="One row"
  7. auto-grow
  8. outlined
  9. rows="1"
  10. row-height="15"
  11. ></v-textarea>
  12. </v-col>
  13. <v-col cols="12" sm="6">
  14. <v-textarea
  15. filled
  16. auto-grow
  17. label="Two rows"
  18. rows="2"
  19. row-height="20"
  20. ></v-textarea>
  21. </v-col>
  22. <v-col cols="12" sm="6">
  23. <v-textarea
  24. label="Three rows"
  25. auto-grow
  26. outlined
  27. rows="3"
  28. row-height="25"
  29. shaped
  30. ></v-textarea>
  31. </v-col>
  32. <v-col cols="12" sm="6">
  33. <v-textarea
  34. filled
  35. auto-grow
  36. label="Four rows"
  37. rows="4"
  38. row-height="30"
  39. shaped
  40. ></v-textarea>
  41. </v-col>
  42. </v-row>
  43. </v-container>
  44. </template>

Textareas(多行文本框) - 图11

美丽的表单

利用替代的输入样式,您可以创建易于构建和使用的惊人界面。

template script


  1. <template>
  2. <v-card
  3. class="mx-auto"
  4. style="max-width: 500px;"
  5. >
  6. <v-system-bar
  7. color="deep-purple darken-4"
  8. dark
  9. >
  10. <v-spacer></v-spacer>
  11. <v-icon small>mdi-square</v-icon>
  12. <v-icon
  13. class="ml-1"
  14. small
  15. >mdi-circle</v-icon>
  16. <v-icon
  17. class="ml-1"
  18. small
  19. >mdi-triangle</v-icon>
  20. </v-system-bar>
  21. <v-toolbar
  22. color="deep-purple accent-4"
  23. cards
  24. dark
  25. flat
  26. >
  27. <v-btn icon>
  28. <v-icon>mdi-arrow-left</v-icon>
  29. </v-btn>
  30. <v-card-title class="title font-weight-regular">Sign up</v-card-title>
  31. <v-spacer></v-spacer>
  32. <v-btn icon>
  33. <v-icon>mdi-magnify</v-icon>
  34. </v-btn>
  35. <v-btn icon>
  36. <v-icon>mdi-dots-vertical</v-icon>
  37. </v-btn>
  38. </v-toolbar>
  39. <v-form
  40. ref="form"
  41. v-model="form"
  42. class="pa-4 pt-6"
  43. >
  44. <v-text-field
  45. v-model="password"
  46. :rules="[rules.password, rules.length(6)]"
  47. filled
  48. color="deep-purple"
  49. counter="6"
  50. label="Password"
  51. style="min-height: 96px"
  52. type="password"
  53. ></v-text-field>
  54. <v-text-field
  55. v-model="phone"
  56. filled
  57. color="deep-purple"
  58. label="Phone number"
  59. ></v-text-field>
  60. <v-text-field
  61. v-model="email"
  62. :rules="[rules.email]"
  63. filled
  64. color="deep-purple"
  65. label="Email address"
  66. type="email"
  67. ></v-text-field>
  68. <v-textarea
  69. v-model="bio"
  70. auto-grow
  71. filled
  72. color="deep-purple"
  73. label="Bio"
  74. rows="1"
  75. ></v-textarea>
  76. <v-checkbox
  77. v-model="agreement"
  78. :rules="[rules.required]"
  79. color="deep-purple"
  80. >
  81. <template v-slot:label>
  82. I agree to the&nbsp;
  83. <a href="#" @click.stop.prevent="dialog = true">Terms of Service</a>
  84. &nbsp;and&nbsp;
  85. <a href="#" @click.stop.prevent="dialog = true">Privacy Policy</a>*
  86. </template>
  87. </v-checkbox>
  88. </v-form>
  89. <v-divider></v-divider>
  90. <v-card-actions>
  91. <v-btn
  92. text
  93. @click="$refs.form.reset()"
  94. >
  95. Clear
  96. </v-btn>
  97. <v-spacer></v-spacer>
  98. <v-btn
  99. :disabled="!form"
  100. :loading="isLoading"
  101. class="white--text"
  102. color="deep-purple accent-4"
  103. depressed
  104. >Submit</v-btn>
  105. </v-card-actions>
  106. <v-dialog
  107. v-model="dialog"
  108. absolute
  109. max-width="400"
  110. persistent
  111. >
  112. <v-card>
  113. <v-card-title class="headline grey lighten-3">Legal</v-card-title>
  114. <v-card-text>
  115. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
  116. </v-card-text>
  117. <v-divider></v-divider>
  118. <v-card-actions>
  119. <v-btn
  120. text
  121. @click="agreement = false, dialog = false"
  122. >
  123. No
  124. </v-btn>
  125. <v-spacer></v-spacer>
  126. <v-btn
  127. class="white--text"
  128. color="deep-purple accent-4"
  129. @click="agreement = true, dialog = false"
  130. >
  131. Yes
  132. </v-btn>
  133. </v-card-actions>
  134. </v-card>
  135. </v-dialog>
  136. </v-card>
  137. </template>
  1. <script>
  2. export default {
  3. data: () => ({
  4. agreement: false,
  5. bio: 'Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts',
  6. dialog: false,
  7. email: undefined,
  8. form: false,
  9. isLoading: false,
  10. password: undefined,
  11. phone: undefined,
  12. rules: {
  13. email: v => (v || '').match(/@/) || 'Please enter a valid email',
  14. length: len => v => (v || '').length >= len || `Invalid character length, required ${len}`,
  15. password: v => (v || '').match(/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*(_|[^\w])).+$/) ||
  16. 'Password must contain an upper case letter, a numeric character, and a special character',
  17. required: v => !!v || 'This field is required',
  18. },
  19. }),
  20. }
  21. </script>

Textareas(多行文本框) - 图12