Merolla
Merolla

Reputation: 315

'This' is undefined

I used Vue-Multiselect https://vue-multiselect.js.org/#sub-getting-started to create a multi select input as follows

<multiselect v-on:select="myfilter()" v-model="cityF" 
:options="cities"  placeholder="City" label="name" track-by="name">

</multiselect>

But this is always undefined inside myfilter function. What is the problem? How can i solve it ? Thanks in adavnce.

Upvotes: 0

Views: 92

Answers (1)

Daniel
Daniel

Reputation: 35694

I'm only guessing, but...

1 You're using literally This instead of this ¯\_(ツ)_/¯

This is easy to fix, just use 'this' instead of 'This'

2 You likely have the function defined as

methods:{
  myfilter: function() {
    console.log(this.whyNoWork);
  }
}

this will not use the correct scope, instead use

methods:{
  myfilter: function() {
    console.log(this.isWorking);
  }.bind(this)
}

or better yet

methods:{
  myfilter() {
    console.log(this.isWorking);
  }
}

3 You should probably define the template as myfilter instead of myfilter(), unless you want to pass something to it

<multiselect v-on:select="myfilter" v-model="cityF" 
:options="cities"  placeholder="City" label="name" track-by="name">
</multiselect>

also more code usually means more help, otherwise we're just guessing.

Upvotes: 2

Related Questions