简体   繁体   English

打字稿变量“未出现在当前范围内”

[英]Typescript variable “does not appear in current scope”

I am trying to use the following code: 我正在尝试使用以下代码:

$('body').bind('ajaxSend', function (elm, xhr, s) {
    if (s.hasContent && securityToken) {   // handle all verbs with content
        var tokenParam = "__RequestVerificationToken=" + encodeURIComponent(securityToken);
        s.data = s.data ? [s.data, tokenParam].join("&") : tokenParam;
        // ensure Content-Type header is present!
        if (s.contentType !== false || options.contentType) {
            xhr.setRequestHeader( "Content-Type", s.contentType);
        }
    }
});

I found this in the post Stack Overflow post 我在堆栈溢出帖子中找到了这个

With typescript I get a syntax error on "options" saying "the name options does not appear in the current scope. 使用打字稿,我在“选项”上收到语法错误,说“名称选项未出现在当前范围内。

Can someone help and explain why I am getting this error. 有人可以帮忙解释我为什么收到此错误的信息。 I see options is not declared and wonder where it comes from if it's not declared. 我看到未声明选项,并且想知道如果未声明它来自何处。

It's just like with JavaScript where, at run-time, any enclosing function scopes and then the global scope are searched for an options variable and if it isn't found anywhere an exception is thrown because it's not defined. 就像JavaScript一样,在运行时,将在所有封闭函数作用域中,然后在全局作用域中搜索一个options变量,如果在任何地方都找不到该变量,则会因为未定义而抛出异常。

So presumably, in the example code you pulled this from, you should assume that options is defined elsewhere, and you'd need to do the same. 因此,大概是在您从中提取示例代码的情况下,您应该假定options在其他位置定义,并且您需要这样做。

I believe the slight mistake is that this line: 我相信这条线是一个小错误:

 if (s.contentType !== false || options.contentType) {

Should actually be: 实际上应该是:

 if (s.contentType !== false || s.contentType) {

As your parameter s is the ajaxOptions passed to your function by jQuery. 作为参数s是jQuery传递给函数的ajaxOptions

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM