search

Home  >  Q&A  >  body text

Observe model properties using Vue 3 and Typescript

<p>I'm trying to monitor my Typescript model properties and it works but gives a warning in the console and I can't find how to remove it. </p> <p>This is my Typescript model: </p> <pre class="brush:php;toolbar:false;">import { watch, ref, Ref, reactive } from 'vue' export default class Resa { public id: Number = 0 public deferred_invoicing: Ref<Boolean> = ref(false) constructor(properties?: Object) { watch(this.deferred_invoicing, (newValue, oldValue) => { console.log(newValue) } } }</pre> <p>The watch is working fine, but I have this warning in the console<code>[Vue warn]: Invalid watch source: false A watch source can only be a getter/effect function, a ref, a reactive object , or an array of these types.</code></p> <p>Did I do something wrong? </p> <p>I have tried using the string <code>'deferred_invoicing'</code> instead of <code>this.deferred_invoicing</code></p>
P粉143640496P粉143640496571 days ago734

reply all(1)I'll reply

  • P粉512363233

    P粉5123632332023-09-03 10:53:10

    Your class instance is set to Reactive somewhere, making its deferred_invoicing attribute unreferenceable

    use

    watch(toRaw(this).deferred_invoicing, (newValue, oldValue) => {
          console.log(newValue)
        }

    reply
    0
  • Cancelreply