djur1
djur1

Reputation: 39

Using method in created hook gives reference error

I'm in the process of learning Vue but I'm having some (basic) issues.

I'm trying to call a method that's defined in the component, when the component renders. For that I'm using the created hook, but I'm getting this in the console: Uncaught ReferenceError: myMethod is not defined.

My component javascript looks like this:

export default {
  name: 'MyComponent',
  methods: {
    myMethod: function () {
      console.log("asd123")
    }
  },
  created: myMethod()
}

If it's relevant for my issue: the component in question is getting rendered through vue-router.

Upvotes: 0

Views: 177

Answers (1)

yuriy636
yuriy636

Reputation: 11661

Use the structure shown in the docs:

export default {
  name: 'MyComponent',
  methods: {
    myMethod: function () {
      console.log("asd123")
    }
  },
  created: function() {
    this.myMethod();
  }
}

Upvotes: 2

Related Questions