admin管理员组文章数量:1392007
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')
})
本文标签:
版权声明:本文标题:javascript - VueJS error "Uncaught TypeError: this.sendDrag is not a function" in mounted () - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744766399a2624067.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论