Angular 7 中的 ActiveXObject 问题

Issues with ActiveXObject in Angular 7

我正在使用 Angular 7 及其 CLI 在网站上开发一个小型 page/router component。有一次我需要检查用户是否允许 flash,我这样做是这样做的:

checkFlash() {
  var hasFlash = false;

  try {
    hasFlash = Boolean(new ActiveXObject("ShockwaveFlash.ShockwaveFlash"));
  } catch (exception) {
    hasFlash = "undefined" != typeof navigator.mimeTypes["application/x-shockwave-flash"];
  }

  return hasFlash;
}

我在 here 上发现了这个,它工作得很好,但现在我正在清理我的应用程序,我注意到 Angular 似乎不喜欢这个,事实上,它说 ActiveXObject 没有定义,但它仍然有效。

Super confused...

我尝试像这样链接一个实际的 flash 对象 $('embed[type="application/x-shockwave-flash"]')$('embed[type="application/x-shockwave-flash"]')[0] 但没有成功,它总是返回 true。

我尝试安装额外的 npm(s),包括 activex-supportactivex-data-support 以及它们的 @types 表兄弟。设置它们后,我发现它们对我的案子没有任何帮助。

Here are the exact errors the CLI & VScode-Intellisense gave me:

VScode:

[ts] 'ActiveXObject' only refers to a type, but is being used as a value here. [2693] any

CLI:

ERROR in src/app/games/games.component.ts(51,30): error TS2304: Cannot find name 'ActiveXObject'.

在纯 JS 中 运行 时不会抛出此错误,但我环顾四周,似乎无法弄清楚如何 运行 pure JSAngular 2 (7)。还看了 here 运气不好。

Please help, completely lost here.

EDIT: Found the fix --> The answer was here listed inside the comments (will need to make minor changes)(shown below)

  • change from:
checkFlash() {
  var hasFlash = false;

  try {
    hasFlash = Boolean(new ActiveXObject("ShockwaveFlash.ShockwaveFlash"));
  } catch (exception) {
hasFlash = "undefined" != typeof navigator.mimeTypes["application/x- 
shockwave-flash"];
  }

  return hasFlash;
}
  • to:
function checkFlash() {
    var hasFlash = false;
    try {
        var flash =
            navigator.mimeTypes &&
            navigator.mimeTypes["application/x-shockwave-flash"]
                ? navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin
                : 0;
        if (flash) hasFlash = true;
    } catch (e) {
        if (navigator.mimeTypes["application/x-shockwave-flash"] != undefined)
            hasFlash = true;
    }

    return hasFlash;
}

答案已列在评论中(需要稍作修改)(如下所示)

更改自:

checkFlash() {
  var hasFlash = false;

  try {
    hasFlash = Boolean(new ActiveXObject("ShockwaveFlash.ShockwaveFlash"));
  } catch (exception) {
hasFlash = "undefined" != typeof navigator.mimeTypes["application/x- 
shockwave-flash"];
  }

  return hasFlash;
}

至:

function checkFlash() {
    var hasFlash = false;
    try {
        var flash =
            navigator.mimeTypes &&
            navigator.mimeTypes["application/x-shockwave-flash"]
                ? navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin
                : 0;
        if (flash) hasFlash = true;
    } catch (e) {
        if (navigator.mimeTypes["application/x-shockwave-flash"] != undefined)
            hasFlash = true;
    }

    return hasFlash;
}

ActiveXObject 的这个问题可以解决,如下所示: 转到您的 tsconfig.json 文件并添加“scripthost”库。 然后重新编译您的应用程序。

 "lib": [
  "es2017",
  "dom",
  "scripthost"
]