如何导出这个覆盖函数,以便导入模块可以检查该函数是否已被调用?
// util.js
export function isPageload() {
return (!!(isPageload = function() { return false; }));
}
当我用 Babel 编译它时,我得到这个错误:
Uncaught TypeError: (0 , _util2.default) is not a function
这是 ES5 的等价物:
var isPageload = function() {
return (!!(isPageload = function() { return false; }));
}
console.log(isPageload()); // true
console.log(isPageload()); // false
最佳答案
错误中的.default
清楚表明你在做什么
import isPageload from 'foo';
当你可能想要的时候
import {isPageload} from 'foo';
自从
export function isPageload() {
创建命名导出,而不是默认导出,并且默认导出实时绑定(bind)更新 currently does not work in Babel .
不过,您解决这个问题的方法似乎有些迂回。为什么不做
let loaded = true;
export isPageLoaded(){
let state = loaded;
loaded = false;
return loaded;
}
关于javascript - ES6 导出覆盖函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32793404/