【问题标题】:Vue Component Button is not being disabled dynamicallyVue 组件按钮没有被动态禁用
【发布时间】:2023-03-06 04:19:01
【问题描述】:

我有一个组件 command-button 包装了一个 Vuetify v-btn 组件。

command-button 接收一个名为 disabled 的布尔型属性,默认为非必需且为 false。

command-button 正在另一个名为 toolbar 的组件中使用,其中在 v-for 循环中,我使用 actions 对象的配置数组添加 command-button 作为属性传递给 toolbar

<command-button
  v-for="(action, index) in actions"
  :key="index"
  :label="action.label"
  :disabled="action.disabled"
  @click="action.handler"
></command-button>

当我在 我的视图组件中使用我的 toolbar 组件时,如下所示:

<toolbar :actions="actions"></toolbar>

我在我的视图组件的数据中声明toolbaractions,如下:

data() {
  return {
    ...        
    actions: [
      {
        label: "Delete",
        handler: this.onDelete,
        disabled: this.disable // This can be a computed or a data element
      },
      {
          label: "Add",
          handler: this.onAdd
      }
    ],
    ...
  },

问题是command-button 没有被动态禁用,无论我使用computed 还是data 中的成员。仅当我在 actions 配置对象中使用文字 true 时才有效。进行一些调试,在toolbar 内部接收到的actions 元素的disabled 属性值为undefined

【问题讨论】:

  • 你得到label属性了吗?
  • 是的,按钮用标签重新渲染没有问题

标签: vue.js vue-component


【解决方案1】:

您不能在 data 对象中引用计算属性,因为在创建实例时不会定义它。您可以在 this.disable 上添加一个观察者,并在 actions[0].disabled 更改时更新它的值。

【讨论】:

  • 我解决了一个事件而不是一个观察者。但事实上,我不能使用计算或其他数据成员作为数据成员的值,这让我很清楚......谢谢!
猜你喜欢
  • 1970-01-01
  • 2021-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多