首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在使我的导入模块出现问题时

在使我的导入模块出现问题时
EN

Stack Overflow用户
提问于 2021-08-30 13:28:39
回答 1查看 71关注 0票数 0

我正在更新Javascript中的一个(雷鸟)扩展。在我的JS文件中,我有:

代码语言:javascript
复制
var { ObjectUtils } = ChromeUtils.import("resource://gre/modules/ObjectUtils.jsm");

现在,我知道var是不受欢迎的,我们喜欢const,而这种导入确实是不变的。但是,如果我使用:

代码语言:javascript
复制
const { ObjectUtils } = ChromeUtils.import("resource://gre/modules/ObjectUtils.jsm");

我在重新定义ObjectUtils时会遇到错误,可能是在我的XUL/XHTML中包含多个JS文件时,这些JS文件中有相同的行。

Som

  • 我应该坚持var吗?
  • 我应该写: if (“未定义的”== typeof( ObjectUtils )) { const {ObjectUtils}=ObjectUtils} ?
  • 我该做点别的吗?

根据普遍的请求,下面是堆栈:

代码语言:javascript
复制
redeclaration of var ObjectUtils removedupes.js:1
    <anonymous> chrome://removedupes/content/removedupes.js:1
    <anonymous> chrome://removedupes/content/overlay-injectors/messenger.js:5
    _loadIntoWindow jar:file:///home/eyalroz/.thunderbird/Profiles/8shkz5up.default/extensions/{a300a000-5e21-4ee0-a115-9ec8f4eaa92b}.xpi!/api/WindowListener/implementation.js:968
    onLoadWindow jar:file:///home/eyalroz/.thunderbird/Profiles/8shkz5up.default/extensions/{a300a000-5e21-4ee0-a115-9ec8f4eaa92b}.xpi!/api/WindowListener/implementation.js:687
    checkAndRunExtensionCode resource:///modules/ExtensionSupport.jsm:220
    _checkAndRunMatchingExtensions resource:///modules/ExtensionSupport.jsm:192
    registerWindowListener resource:///modules/ExtensionSupport.jsm:71
    forEach self-hosted:4357
    registerWindowListener resource:///modules/ExtensionSupport.jsm:70
    startListening jar:file:///home/eyalroz/.thunderbird/Profiles/8shkz5up.default/extensions/{a300a000-5e21-4ee0-a115-9ec8f4eaa92b}.xpi!/api/WindowListener/implementation.js:569
    startListening self-hosted:1175
    result resource://gre/modules/ExtensionParent.jsm:935
    withPendingBrowser resource://gre/modules/ExtensionParent.jsm:491
    result resource://gre/modules/ExtensionParent.jsm:935
    callAndLog resource://gre/modules/ExtensionParent.jsm:897
    recvAPICall resource://gre/modules/ExtensionParent.jsm:934
    InterpretGeneratorResume self-hosted:1482
    AsyncFunctionNext self-hosted:692
EN

回答 1

Stack Overflow用户

发布于 2021-08-30 13:38:44

试试这个:

代码语言:javascript
复制
const { ObjectUtils: Cu } = ChromeUtils;
Cu.import("resource://gre/modules/ObjectUtils.jsm");

这个帖子可以帮上忙。

从我可以收集到的信息来看,这增加了一种导入ObjectUtils的方法,而无需为常量赋值(在运行时不能更改)。

我是这样想的:

代码语言:javascript
复制
// say you create a constant array (I know its not an array - just an example)
const cars = ["Saab", "Volvo", "BMW"];

// you can add an element, just like you can add an import
cars.push("Audi");
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68984986

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档