-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Vue reactivity doesn't work with ES private fields #7240
Comments
ES6 proxies simply don't support private fields which is a huge PITA., but consequently this is unfortunately a wontfix, or rather "can't fix". FWIW, we always recommend to use POJOs for reactive state. We need to be more explicit about this specific hard limitation in the docs, though. |
Not really an option after. all. FWIW, you can hack around the limitation with things like: class X {
#a = 'secret'
#b = 'secret'
getPropA = () => this.#a
constructor() {
this._self = markRaw({ self: this })
}
get getA() { return this.getPropA() }
get getB() { return this._self.self.#b }
} |
@LinusBorg Can please elaborate on that? What's does "simply don't support" mean? The Proxy doesn't have access to private members but that's by design -- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy#no_private_property_forwarding |
The docs I've read explicitly recommend adding methods for stuff:
It seems like a fairly reasonable recommendation. The alternative is a very different organization of code when you've got semi complex state and procedures needed to access and mutate it. It's like Redux style state+actions. |
Sure its by design - a design that means we can't classes that contain native private fields with proxies - which means such classes don't (reliably) work with our reactivity system. |
The docs you link here still use a plain object (just one with methods on it), not a class, and certainly not a class with private fields |
Oh OK by POJO I thought you meant something basically something JSON serializable. I'm probably using "POJO" wrong. But, isn't a class instance just an object with methods on it? By "POJO" you mean I can't use some object constructed from a class constructor? |
Not really. Since ES6, classes are a distinct thing in JS. One of those distinctions is: classes support native private fields.
I'ts recommended not to. Though using a class usually works fine as long as the classes are simple enough and only manage their own property-based state. One thing that simply does not work - rhe thing this issue is about - is that classes with private properties dont play nice with Proxies. So they cant be used with Vue's Reactivity. |
Vue version
3.2.45
Link to minimal reproduction
https://codepen.io/taras-turchenko/pen/JjZvBep
Steps to reproduce
What is expected?
Reactivity works
What is actually happening?
It causes an error:
When code is processed by babel
System Info
Any additional comments?
We want to migrate a front-end project from Vue2 to Vue3 and have many classes and OOP code. We've been using private fields to encapsulate our logic for a year. Now we process private fields by babel but it reproduces as well in native & babel processed code
Stackoverflow topic: https://stackoverflow.com/a/68731963
The text was updated successfully, but these errors were encountered: