工作随笔:vue 使用引入的公共方法(utils)时报错: Uncaught TypeError: Object(...) is not a function

89 阅读1分钟

utils.js

const throttle = (fn, gapTime, ctx) => {
    let lock = true
    return function(){
        if(!lock) return
        lock = false
        setTimeout(() => {
            fn.apply(this, arguments)
            lock = true
        }, gapTime)
    }
    
}

export default { throttle }

然后在vue文件中引入 utils.js 文件

import throttle from './script/utils'

methods: {
    throttleBtn:throttle(function(e){
        console.log('我被点击了~')
    },1000)
  }

看起来没问题,然后报错了

1656490293455.jpg

最后把调用地方改成这样就可以了

import utils from './script/utils'

methods: {
    throttleBtn:utils.throttle(function(e){
        console.log('我被点击了~')
    },1000)
  }

分许:通过 import { throttle } from './script/utils' 引入的默认是对象而不是函数。