You should never use an arrow function to define a method with Vue, because with arrow functions, this
is defined lexically.
An arrow function does not create its own this, the this value of the
enclosing execution context is used.
That means that, with Vue methods, this
will point to the global object, or the window.
When a Vue is instantiated, Vue binds the methods defined in methods
to the Vue object. An arrow function cannot be bound. It's this
will always be the what it was originally.
That being the case, you should always use a normal function to write a Vue method.
This also applies to the data function, computed values, and lifecycle hooks. The only time you will generally use an arrow function is when you are defining a function inside one of your methods or computed values, as you might when defining a callback for an ajax call.
See VueJS: why is “this” undefined?.
Note: there is one exception to never using an arrow function to define a Vue method and that would be if you never referenced this
at all in the method. Those cases are exceedingly rare. You almost always end up needing to reference other parts of the Vue inside a method using this
. That being the case, it should quickly become a habit to simply never define a Vue method with an arrow function.