【问题标题】:Vue prop watch not working in class styled componentVue prop watch 在类样式组件中不起作用
【发布时间】:2020-12-18 20:27:48
【问题描述】:

我有一个名为 masterDatasourceId 的属性,如下所示,我在 ajax 调用中从父组件传递。

但是 watch 方法没有被调用。以下是我的代码。

  @Prop()
  masterDatasourceId!: number;

  @Watch('masterDatasourceId')
  masterDatasourceIdChanged(newVal: number) {
    // Even though masterDatasourceId changes, it is not coming up here.
  }

我在这里缺少什么?我正在使用基于类样式的打字稿的 vue 组件。

我想使用下面的 getter setter 方法来获取它。

  private _masterDatasourceId!: number;
  public get masterDatasourceId(): number {
    return this._masterDatasourceId;
  }

  @Prop()
  public set masterDatasourceId(value: number) {
    console.log('this I wanted to use as a watcher if possible...!!!')
    this._masterDatasourceId=value;
  }

但它给出了以下运行时错误。

The computed property "masterDatasourceId" is already defined as a prop.

【问题讨论】:

    标签: typescript vue.js vue-component


    【解决方案1】:

    这似乎需要立即为真。在此更改后工作

    @Watch('masterDatasourceId', { immediate: true })
      masterDatasourceIdChanged(newVal: number) {
    }
    

    【讨论】:

      猜你喜欢
      • 2021-10-04
      • 2021-06-06
      • 2023-01-08
      • 2021-04-24
      • 2022-01-21
      • 1970-01-01
      • 2018-02-20
      • 2017-06-28
      • 1970-01-01
      相关资源
      最近更新 更多