我不知道为什么我在控制台中获得undefined
。我阅读了关于范围变量的知识,并且知道在范围外定义一个变量应该使该变量可以在特定函数范围之外访问。 不知道我在做什么错在这里:未定义的值,变量范围问题
const history = createHistory();
const location = history.location;
let loc;
const listen = history.listen((location) => {
loc = `${location.search}${location.hash}`;
return loc;
})
console.log(loc);
我的控制台登录undefined
请注意,'location'是'window'上的一个全局变量,您可以通过'window.location'或甚至'location'访问它。 – JohnnyCoder