// index.js
export const test = () => {
let count = 1
return () => {
console.log(count++)
}
}
// index.vue
import { test } from './index.js'
test() // 没有任何打印
// test.js
import { test } from './index.js'
test()
test()
如果是在js中调用结果是 1 2
暂且使用的办法是将闭包函数改为立即执行,但是这种情况执行
export const test = () => {
let count = 1
return (() => {
console.log(count++)
}}()
}
// .vue
mounted() {
test()
test()
}
在vue中打印的结果是 1 1
var fn = test()
fn() // 1
fn() // 2