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

'This' object inside vue instance methods is undefined #6

Closed
CaptainYouz opened this issue Apr 23, 2016 · 7 comments
Closed

'This' object inside vue instance methods is undefined #6

CaptainYouz opened this issue Apr 23, 2016 · 7 comments

Comments

@CaptainYouz
Copy link

CaptainYouz commented Apr 23, 2016

Hi !

I have also tested on the browserify vuejs-templates and i have the same error.
I followed the README and have set up a new project using vue-cli browserify, but when i'm trying to reach the 'this' object inside vue instance method (created, beforeCompile...), it always return undefined into the console.

Here is an example when i modify the script part of the App.vue file (it's the only one that i modified).

export default {
  data () {
    return {
      // note: changing this line won't causes changes
      // with hot-reload because the reloaded component
      // preserves its current state and we are modifying
      // its initial state.
      msg: 'Hello Vue!'
    }
  },
  created: () => {
    console.log('created', this);
  },
  beforeCompile: () => {
    console.log('beforeCompile', this);
  },
  compiled: () => {
    console.log('compiled', this);
  },
  ready: () => {
    console.log('ready', this);
  }
}

On each log, this is undefined.

screen shot 2016-04-23 at 1 44 37 pm

I'm am missing something ?

Configuration: Macbook Pro on Chrome v50.0.2661.75 (64-bit)

@chrisvfritz
Copy link
Contributor

chrisvfritz commented Apr 24, 2016

@CaptainYouz This happens because the ES6/2015 arrow function syntax (() => {}) binds this to the parent context. In this case, the parent context is the module. Since this is undefined in the module context, that undefined value is passed to each of the lifecycle methods instead of the view model as you'd expect.

To fix this, you'll need to use a normal function declaration:

created: function () {
  console.log('created', this);
},

Or, my personal preference is the ES6/2015 shorthand, which does the same as above:

created () {
  console.log('created', this);
},

@CaptainYouz
Copy link
Author

CaptainYouz commented Apr 24, 2016

Thanks for the explanation !
It's working now 👍 .

@jgb-solutions
Copy link

great!

@taoliujun
Copy link

but this is still link methods.but not vm.

@imageslr
Copy link

Thanks a lot!!!

@theluk
Copy link

theluk commented Nov 17, 2017

how are you dealing with methods you need to bind to this. for example when you want to wrap a method in lodash´s debounce?

@chrisvfritz
Copy link
Contributor

@theluk You don't need to do anything special. Just wrapping the function with debounce should work, e.g.:

myMethod: debounce(
  function () {
    // `this` is the app/component instance here
  },
  500
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants