-
-
Notifications
You must be signed in to change notification settings - Fork 8.5k
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
Adding to this.$options.computed during beforeCreate
only works if computed
is already defined on component
#2791
Comments
only because they are pre defined in the prototype, not define for each instance. and only root instance ( why not do below simply in your sample?: const mixin = {
computed: {
value: () => 'works'
}
} |
The example was a simplification, so it's not that simple. I encountered this bug when upgrading from Vue 2 where this used to work. In our app we have a global mixin that looks at another $options to generate computed properties. Something like: const mixin = {
beforeCreate() {
const magicOptions = this.$options.magic;
for (const alias in magicOptions) {
// subscribe to kv store
const getterAndSetter = subscribe(magicOptions[alias]);
this.$options.computed[alias] = getterAndSetter;
}
},
unmounted() {
// unsubscribe when
this.$options.magic.forEach(unsubscribe);
}
} Then any component can define a "magic" object to subscribe to the backend, and subscription is stopped when component is removed. export default {
magic: {
state: 'some-key-in-the-kv-store',
},
mounted() {
console.log('state', this.state);
},
methods: {
start() {
this.state = 'start';
}
}
} I might be doing this the wrong way, so any suggestions are welcome. |
@edison1105 I think your solution is not completely,take a look about this #2839 |
I see. I think add a hook |
Add a lifecycle hook
|
We ran into the same issue in our app where we're creating a validation helper similar to Vuelidate. We define a We ran into two problems:
I believe this could be fixed by only defining const computedOptions = options.computed;
if (computedOptions) {
... This way if a
For example, adding to the example from @MiniGod: const mixin = {
computed: {},
beforeCreate() {
if (condition) {
this.$options.computed.example = () => {
return 'works';
}
}
}
} In theory this fixes the issue, however since This may be something to be added in a separate issue, I only bring it up because it's a major issue in the only workaround I can see to this issue. |
I found a terrible (but working) solution: // in beforeCreate-hook
const foo = computed(getter/setter)
Object.defineProperty(this, 'foo', {
enumerable: true,
configurable: true,
get: ()=> foo.value,
set: (v)=> foo.value=v
}) |
This has been fixed by e2ca67b |
Version
3.0.4
Reproduction link
https://jsfiddle.net/6k3nod2c/
Steps to reproduce
Create a component / app with a mixin that adds to
this.$options.computed
.What is expected?
Expect
this.value
to be"works"
.What is actually happening?
this.value
isundefined
.In vue 2 this worked.
In vue 3 this only works if the
computed
option is already defined.The text was updated successfully, but these errors were encountered: