Skip to content

Conversation

@itsMapleLeaf
Copy link
Contributor

Adding an undefined property to methods on vue components leads to a very ambiguous error message: Uncaught TypeError: Cannot read property 'length' of undefined

Example:

import stuff from './stuff'

export default {
  template: '...',
  methods: {
    someMethod: stuff.someMetod // typo!!!
  }
}

It's an easy mistake to make. This checks for undefined properties and warns if it finds one.

if (methods[key] != null) {
vm[key] = bind(methods[key], vm)
} else {
warn(`The method ${key} on vue instance is undefined.`, vm)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add else if (process.env.NODE_ENV !== 'production') here so that the warning can be stripped?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

@yyx990803 yyx990803 merged commit 724a593 into vuejs:next Sep 11, 2016
@itsMapleLeaf itsMapleLeaf deleted the kingdaro-init-methods-fix branch September 11, 2016 14:59
if (methods[key] != null) {
vm[key] = bind(methods[key], vm)
} else if (process.env.NODE_ENV !== 'production') {
warn(`The method ${key} on vue instance is undefined.`, vm)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

curious why do you provide vm as a second variable? it will log vm?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's used to get the component name, if available:

https://github.com/vuejs/vue/blob/dev/src/core/util/debug.js#L10-L13

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh I get it, You saved me!!!!!!!!!!

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

Successfully merging this pull request may close these issues.

4 participants