其他分享
首页 > 其他分享> > es6 bind(this) 方法

es6 bind(this) 方法

作者:互联网

参考:https://blog.csdn.net/m0_38060839/article/details/83508863

 

案例

  mounted() {
    this.getindex().then((res) => {
      console.log(res, '111111')
    })
  },
  methods: {
    getindex() {
        
      // return new Promise((reslove, reject) => {              //箭头函数无需绑定this
      //   setTimeout(() => {
      //     reslove(this.handlename)
      //   }, 1000)
      // })

      return new Promise(function(reslove, reject) {
        setTimeout(() => {
          reslove(this.handlename)
        }, 1000);
      }.bind(this))                                           // 普通函数绑定this
} }

 

标签:getindex,es6,res,reject,reslove,bind,new,setTimeout,方法
来源: https://www.cnblogs.com/shun1015/p/15717493.html