I can't figure out why this.sendDrag('started')
returns this error:
"Uncaught TypeError: this.sendDrag is not a function"
methods: {
sendDrag (val) {
console.log(val)
}
[...]
mounted () {
this.$nextTick(() => {
this.$refs.flickity.on('dragStart', function () {
this.stageDragging = true
this.sendDrag('started')
})
What causes the error and how to fix it?
I can't figure out why this.sendDrag('started')
returns this error:
"Uncaught TypeError: this.sendDrag is not a function"
methods: {
sendDrag (val) {
console.log(val)
}
[...]
mounted () {
this.$nextTick(() => {
this.$refs.flickity.on('dragStart', function () {
this.stageDragging = true
this.sendDrag('started')
})
What causes the error and how to fix it?
Share Improve this question asked Apr 30, 2019 at 20:01 TomTom 6,03421 gold badges85 silver badges134 bronze badges 3- i think it is because this scoping. U could use arrow function ... or use self helper. I mean arrow function insted of function() – curious lad Commented Apr 30, 2019 at 20:04
- I don't understand what you mean. Can you please provide an example? – Tom Commented Apr 30, 2019 at 20:28
- 1 Here is the solution to your problem: stackoverflow./a/55836450/10251861 – sml-sgr Commented Apr 30, 2019 at 20:47
1 Answer
Reset to default 8You need to capture the value of this
in the closure because you are calling it from a function that has a different this
.
If you used the arrow-lambda notation ()=>{}
instead of function()
it would capture this
for you automatically. And that is the real difference between the two notations.
mounted () {
this.$nextTick(() => {
const that = this;
this.$refs.flickity.on('dragStart', function () {
that.stageDragging = true
that.sendDrag('started')
})