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

Calling methods inside of data() is not allowed in 2.x? #4302

Closed
tzurbaev opened this issue Nov 24, 2016 · 6 comments
Closed

Calling methods inside of data() is not allowed in 2.x? #4302

tzurbaev opened this issue Nov 24, 2016 · 6 comments

Comments

@tzurbaev
Copy link

Vue.js version

2.1.0

Reproduction Link

http://jsfiddle.net/ds8o0kry/

Steps to reproduce

Call any method inside of data() function

What is Expected?

Method should be called (just like in 1.x)

What is actually happening?

Uncaught TypeError: %method% is not a function is thrown

We are migrating from vue 1.0.28 to 2.x and seems that data() behaviour changed but there's no info in migration guide, vue-migration-helper also says nothing about this. Is this a bug or possibility of calling methods inside data() was removed?

@mbj36
Copy link

mbj36 commented Apr 27, 2018

Hey @tzurbaev I am looking for solution, how can we achieve this ?

@tzurbaev
Copy link
Author

@mbj36 this bug was fixed in v2.1.4, so you need to update Vue if you're still on old version.

@ijunaid8989
Copy link

we still cannot call a method within a data?

@pauliuuso
Copy link

We can't call it too, on 2.5.21 version

@ijunaid8989
Copy link

Then why this issue is closed?

@jonasnordlund
Copy link

jonasnordlund commented Feb 28, 2020

This does work. Pay attention to the posted jsfiddle. It opened with Javascript in the "no library (pure JS)" mode. As soon as I picked VueJS 2.2.1 from there (confirmed to work on 2.6.10 as well), it started giving the expected result.

If you still have issues with this in your code, pay special attention to the data object. Like in the posted jsfiddle, it needs to be returned from a function and not directly declared with the Javascript object notation as a property. If you do this, calling functions from within should work as well. You'll easily run into limitations with this though; the VueJS state will be before created() has been called.

@vuejs vuejs locked as resolved and limited conversation to collaborators Feb 28, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants