首页 > 解决方案 > 在默认插槽中动态更改 v-text-field 中的类

问题描述

我正在尝试根据文本的长度动态更改 v-text-field 默认插槽中的字体大小。但是,似乎 v-text-field 忽略了我在该部分中指定的任何规范。

这是代码

        <v-text-field
          v-model="attr.name"
          hide-details
          :readonly="true"
          class="core-select"
          label="Core Attribute"
        >
          <template
            v-slot:default
          >
            <div :class="attrNameStyle[0]">
              {{ attr.name }}
            </div>
          </template>
        </v-text-field>

我已经验证 attrNameStyle[0] 设置正确,但是该样式永远不会应用于默认插槽。我可以通过这个 CSS 类改变输入槽的外观,.v-text-field__slot input { ... }但是我不能动态更新那个 CSS。

感谢帮助!

编辑:添加更多上下文。

.core-select {
  width: 180px;
}

.short-core-select {
  font-size: 12px;
}

attrNameStyle[0] 设置为 '' 或 'short-core-select'。

标签: vuetify.js

解决方案


由于v-text-field__slot工作正常,您可以从更高级别编辑该 CSS。

<v-text-field
      v-model="attr.name"
      hide-details
      hide-details
      class="core-select"
      :class="attrNameStyle[0]"
      label="Core Attribute"
    >
      <template>
        <div>
          {{ attr.name }}
        </div>
      </template>
    </v-text-field>
<style>
  .short-core-select .v-text-field__slot {
    font-size: 12px;
  }
</style>

推荐阅读