Skip to content
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

"computed" property is called immediately #79

Closed
mickeyvip opened this issue Aug 24, 2019 · 5 comments
Closed

"computed" property is called immediately #79

mickeyvip opened this issue Aug 24, 2019 · 5 comments
Labels
bug Something isn't working

Comments

@mickeyvip
Copy link

mickeyvip commented Aug 24, 2019

I am trying to play with the new composition API.

Installed the plugin and copied the "Basic example" from https://vue-composition-api-rfc.netlify.com/#basic-example.

It throws an exception inside the computed getter.

[Vue warn]: Error in setup(): "TypeError: Cannot read property 'count' of undefined"

found in

---> <Counter> at src/components/Counter.vue
       <App> at src/App.vue
         <Root>

The state is declared as follows:

setup() {
    const state = reactive({
      count: 0,
      double: computed(() => state.count * 2)
    })
   //...
}

Am i doing something wrong or is this not supported yet in the plugin?

Thank you.

@doncatnip
Copy link

It would seem that the computed property is called immediately, so state is not known yet.
This should work:

  const state = reactive({
    count: 0,
  })
  state.double = computed(() => state.count * 2)

@mickeyvip
Copy link
Author

@doncatnip , thank you.

Seems like this plugin is not 100% compliant with the RFC?

@doncatnip
Copy link

doncatnip commented Aug 24, 2019

It certainly isn't. Some shortcomings are documented. This one isn't (if it is indeed one, and not a mistake in the example). It might just be a bug as I would guess computed should only be evaluated on demand.

@mickeyvip
Copy link
Author

Got it.

Thank you

@liximomo liximomo added the bug Something isn't working label Aug 25, 2019
@liximomo liximomo changed the title Throws exception on the "Basic Example" inside "computed" getter "computed" property is called immediately Aug 25, 2019
@edwardnyc
Copy link
Contributor

您好,我没理解到为什么使用Object.seal可以解决这个问题呢。 可以指导下嘛
@liximomo
Hello, I did not understand why using Object.seal can solve this problem. Can you guide me

@vuejs vuejs locked and limited conversation to collaborators Aug 17, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants