【问题标题】:How to add @change to a custom Vue component如何将 @change 添加到自定义 Vue 组件
【发布时间】:2021-11-11 15:51:54
【问题描述】:

我四处寻找,找不到解决问题的方法,所以我希望有人能提供帮助。

我已经创建了几个自定义表单组件,但我仍然必须恢复到“原始”代码来完成一些任务,因为我找不到在自定义 select 上使用 @change 的方法。

这是我平时做的事情……

自定义select 组件 - 省略了道具和方法:

<v-select v-if="inputType === 'select'"
                  @input="updateValue($event)"
                  :background-color="backgroundColor ? 'backgroundColor' : 'white'"
                  class="rounded-lg"
                  :clearable="clearable"
                  :chips="chips"
                  color="accent2"
                  :deletable-chips="deletableChips"
                  :disabled="disabled || isFormReadOnly"
                  :error="error"
                  :error-messages="errorMessages"
                  filled
                  flat
                  hide-details="auto"
                  :items="items"
                  :item-text="itemText && itemText"
                  item-value="value"
                  :label="label"
                  :multiple="multiple"
                  :outlined="!outlined && !isFormReadOnly"
                  :return-object="returnObject"
                  :small-chips="smallChips"
                  :value="value"/>

...我这样称呼它:

<app-input input-type="select"
           class="rounded-lg mb-4"
           :class="$vuetify.breakpoint.width >= 600 && 'pl-2'"
           :error="errors.type"
           :is-form-read-only="isFormReadOnly"
           :items="['Drill', 'Emergency']"
           label="Type"
           v-model="form.type"/>

...效果很好,但我无法使用 @change 调用更改函数。

有什么想法吗?

【问题讨论】:

    标签: vue.js vuetify.js v-select


    【解决方案1】:

    您是否尝试过选择组件:

    @change="$emit('change', $event.target.value)"
    

    【讨论】:

    • 是的,这样尝试过,但在这种情况下不起作用:(
    【解决方案2】:

    我想你可以试试$emit 或者在实例中写一个watch

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    • 是的,这样尝试过,但在这种情况下不起作用:(
    猜你喜欢
    • 2017-09-26
    • 1970-01-01
    • 1970-01-01
    • 2019-08-18
    • 1970-01-01
    • 1970-01-01
    • 2020-07-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多