最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - VueJS error "Uncaught TypeError: this.sendDrag is not a function" in mounted () - Stack Overflow

programmeradmin1浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 8

You 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')
          })

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论