vue 中setTimeout问题

1.this指向问题

(1)var vm_target = new Vue({

                el: '#vm_target',

                data: {

                    clickSubmitBtn:false

                },

                methods:{

                    myFunc:function(){

                        setTimeout(function(){

                        vm_target .clickSubmitBtn = true; //修改此处(而不是this.clickSubmitBtn = true; //这样修改data中的参数时无效)

                        },500);

                    }

                }

             })

(2)

export default {

 methods: {

  start: function () {

  let _this=this(在setTimeout重新定义变量使其值为this

   setTimeout(function()  {

    _this.end()//

   }, 4000);

  }

 }

}

(3)

export   default {

 methods: {

start:function() {

   setTimeout(() => {

    this.end()//(使用箭头函数)

   }, 4000);

  }

 }

你可能感兴趣的:(vue 中setTimeout问题)