Vue: Calling methods inside of data() is not allowed in 2.x?

Created on 24 Nov 2016  ·  6Comments  ·  Source: vuejs/vue

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?

improvement

Most helpful comment

Then why this issue is closed?

All 6 comments

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

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

we still cannot call a method within a data?

We can't call it too, on 2.5.21 version

Then why this issue is closed?

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

julianxhokaxhiu picture julianxhokaxhiu  ·  3Comments

6pm picture 6pm  ·  3Comments

bfis picture bfis  ·  3Comments

robertleeplummerjr picture robertleeplummerjr  ·  3Comments

bdedardel picture bdedardel  ·  3Comments