vuejs / vue-class-component

ES / TypeScript decorator for class-style Vue components.
MIT License
5.81k stars 429 forks source link

[vue3] how do you declare a class property that isn't reactive? #583

Open runxc1 opened 2 years ago

runxc1 commented 2 years ago

I can't seem to find any vue3 docs. I'm wanting to basically create a private variable that isn't reactive and hasn't been altered but that I can still use in the class.

zdevwu commented 2 years ago

@runxc1 did you manage to find out any solution?

zdevwu commented 2 years ago

Figured it out, you need to use markRaw or shallowRef, I created an attribute like this for shallowRef so it works better with typing system:

import { shallowRef, makeRaw } from 'vue'

export function ShallowRef () {
  return (target: any, propertyKey: string) => {
    target[propertyKey] = shallowRef(target[propertyKey])
  }
}

class SomeComponent extends Vue {
   @ShallowRef()
   private targetProperty = {some: 'value'}

   private noReactiveAtAll = markRaw({something: 'else' })
}