- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我在 React 中遇到了一个问题,如果我尝试使用“历史”,我的代码将不会运行,并告诉我这是“意外使用 history
no-restricted-global” .
我求助于 StackOverflow 以获得帮助,令人惊讶的是,我能够找到我正在处理的问题的答案。
另一个用途建议的修复非常有效,但现在我很困惑为什么它首先起作用。
使用 redux,我通过 mapDispatchToProps 调用了一个方法。我需要将“历史记录”作为传入的变量之一放入,以便我可以将用户重定向回他们所在的上一页。
最初,我尝试仅使用“历史记录”本身作为变量,但在尝试编译时会出现“无限制全局”错误,因此我求助于 StackOverflow。这是他们建议我尝试使用“window.history”的时候。起初我持怀疑态度,因为我认为这么简单的事情无法解决我的问题,但你瞧,它编译成功了。
props.addExperience(data, history);
对比
props.addExperience(data, window.history);
window.history 有效。有人可以解释为什么 window.history 有效但 history 本身不起作用吗?
“历史”到底是什么? “窗口”做了什么来解决这个问题?
编辑:this是我找到的原始 StackOverflow 帖子。虽然 Chasen Bettinger 能够解决这个问题,但他从未真正解释过为什么这个窗口。会解决这个问题。如果这里有人能做到这一点,那就太棒了。
最佳答案
您遇到的 linting 规则试图解决的问题是隐式引用全局对象上的属性是 easy source of bugs .例如:
var status = false;
if (status) {
console.log('status is actually truthy!');
}
这里,顶层的status
实际上是指window.status
,它必须始终是一个字符串;将 false
分配给它会导致 false
变成 'false'
。
如果 status
是具有该规则的受限全局,则只有在明确引用 window.status 时才能在顶层使用
。这样一来,很明显您故意引用了全局属性,这并非偶然。status
当您引用 window.history
而不是 history
时,会发生同样的事情。例如,如果您在代码的前面定义了一个变量会怎么样
var history;
然后使用
props.addExperience(data, history);
linter 不确定您是在尝试引用全局对象上的属性,还是输入有误。因此,规则是让您明确指定该属性位于 window
上(或更正变量名称)。
至于window.history
到底是什么,see MDN :
The
Window.history
read-only property returns a reference to the History object, which provides an interface for manipulating the browser session history (pages visited in the tab or frame that the current page is loaded in).
例如
history.back(); // equivalent to clicking back button
history.go(-1); // equivalent to history.back();
window.history.go(0); // refresh the current page
history.pushState(stateObj, "page 2", "bar.html"); // add an item to the history state
关于javascript - “历史”、无限制全局变量和 window.history,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56718921/
我在正则表达式帖子中进行了大量搜索,但没有找到我正在寻找的解决方案。 我有正则表达式 ([a-zA-Z]{6}[a-zA-Z0-9]{2}([a-zA-Z0-9]{3})?) ?接受这些案例: 空
我想获取总金额最大的用户的user_id和总金额。我无法使用 LIMIT,因为这只会返回 1 条记录(多个用户的总金额可能相同) 这是我的数据架构和一些记录 CREATE TABLE transact
我想从 x 开始。 xpage 应用程序(比如 10 对)上的字段(在我的应用程序中我有一对文本数据字段和数字数据字段),然后当用户单击“更多字段”时,我希望更多对动态显示而没有完整刷新页面,但想无限
#include struct B { int x,y; }; struct A : public B { // This whines about "copy assignment ope
是的,我想删除所有 ulimits 并将它们设置为unlimited。我该怎么做呢?谢谢! 最佳答案 使用这样的东西: for opt in $(ulimit -a | sed 's/.*\-\([
我是 C 编程的初学者,我想知道如何才能做到这一点。我希望能够输入整数(我想要的数量),然后找到其中最小的一个。我应该/如何做到这一点?我需要使用数组来存储它们吗?我不太确定如何做到这一点。 谢谢!
我一直在寻找,但似乎编译器开发人员对支持这些的兴趣为零。 对我来说,这似乎很奇怪——基本上,当前的 C++ 对 union 有一些限制,这些限制总是令人恼火而且从不合适。您可能认为基本上删除一些错误检
一个不受限制的 union 类的例子,它包含一个整数映射或一个整数 vector : #include #include #include #include typedef std::vect
谁能解释一下如何将一个字符串(在一个结构体中的 union 体中)赋值并推送到一个 vector 中?这可能吗? vector 是否试图访问无效内存? #include #include #inc
考虑这段代码: struct TNumeric { bool Negative; wstring Integral; wstring Fraction; }; union
我目前正在通过 C# 控制台应用程序使用 Azure API 为 Cosmos DB 播种。在此过程中,我删除并重新创建数据库,以确保删除所有预先存在的数据(这可能不是最好的选择,但我没有看到截断所有
我是一名优秀的程序员,十分优秀!