【问题标题】:How to resolve 'SyntaxError: export declarations may only appear at top level of a module' when exporting variables?导出变量时如何解决“语法错误:导出声明​​只能出现在模块的顶层”?
【发布时间】:2019-08-11 23:45:51
【问题描述】:

我是编程新手,目前正在使用 HTML、CSS 和 JavaScript 构建项目。我想将变量从我的一个外部 JavaScript 文件导出到另一个。但是,我在尝试 Mozilla Firefox 时从控制台收到错误消息。错误是:“SyntaxError:导出声明​​只能出现在模块的顶层”。

我尝试在代码的开头、结尾和函数内部(我希望从中导出它)导出。我在网上看过,但我似乎找不到任何答案,只有导入的答案。

function exports() {
    export { cakeChoicePricing, cakeTypeResultPricing };
    export { cupcakeChoicePricing, cupcakeTypeResultPricing };
};

在下面导入:

import { cakeChoicePricing, cakeTypeResultPricing } from './JavaScript.js';
import { cupcakeChoicePricing, cupcakeTypeResultPricing } from './JavaScript.js';

感谢您提供的任何帮助!

更新(这里是我的更多代码):

let cakeChoicePricing;
let cupcakeChoicePricing;
function dessertChoiceCake() {
        cakeElement.setAttribute('class', 'disabled'); //Set cake button to unclickable
        cakeChoicePricing = 'Cake';
        cupcakeChoicePricing = 'Cake';
}
let exportCake = document.getElementById("cakeReviewButton");
let exportCupcake = document.getElementById("cupcakeReviewButton");

exportCake.addEventListener("click", exports);
exportCupcake.addEventListener("click", exports);

function exports() {
    export { cakeChoicePricing, cakeTypeResultPricing };
    export { cupcakeChoicePricing, cupcakeTypeResultPricing };
};

【问题讨论】:

  • 你是什么意思,你想从一个函数中导​​出导出?你能发布更多关于你想如何使用你的exports函数的上下文吗?
  • @CertainPerformance 我没有在这里显示,但我想在单击某个按钮后运行导出函数,然后将变量传递到另一个文件,我可以在其中显示它们的值。我将导出放在一个函数中,因为我只想在它们通过我的代码的其他部分修改后导出它们。我希望这能进一步澄清一些事情,但如果没有,请告诉我。感谢您的帮助。
  • 您能否发布相关代码,以便我们可以看到(在代码中)您正在寻找的那种控制流?即使它不起作用,它也可能让我们更好地了解您的目标是完成什么
  • 错误信息很清楚——你不能从一个函数中导​​出,这不是顶级代码(不要认为顶级在文件的开头——顶部- 级别代码从 正确缩进的代码 中的一行的第一列开始 - 你不能有条件地导出 - 当你从顶级代码导出时很明显 - 重新考虑你的代码,重新-想想进口方面
  • @CertainPerformance 当然,我刚刚在问题中发布了它。谢谢。

标签: javascript export


【解决方案1】:

考虑反转您的控制流。与其尝试导出尚不存在的变量,不如从另一个文件导入一个函数,然后在需要时使用cakeChoicePricing 等参数调用该函数。例如:

import displayPrices from './prices';

const prices = {};

// when you need to, assign to properties of the prices object:

function dessertChoiceCake() {
  cakeElement.setAttribute('class', 'disabled'); //Set cake button to unclickable
  prices.cakeChoicePricing = 'Cake';
  prices.cupcakeChoicePricing = 'Cake';
}

const callDisplayPrices = () => {
  displayPrices(prices);
};

exportCake.addEventListener("click", callDisplayPrices);
exportCupcake.addEventListener("click", callDisplayPrices);

并让displayPrices 函数(另一个文件导出)处理具有价格属性的对象,例如

export default (prices) => {
  console.log('cakeChoicePricing:', prices.cakeChoicePricing);
};

【讨论】:

  • 感谢您的回复!我只是有几个问题,如果你不介意的话。 displayPrices 到底应该做什么?而且,callDisplayPrices 是函数还是变量(或其他)?最后,prices.cakeChoicePricing = 'Cake'; 到底做了什么?我也很难理解console.log 部分。非常感谢您的帮助。
  • displayPrices 是您导入的函数。在其中,您放置代码以显示价格。 callDisplayPrices 是绑定到函数的变量名,在调用该函数时,会使用 prices 对象调用 callDisplayPrices。 (将callDisplayPrices 放入其自己的变量名有助于保持代码干燥,否则您必须在按钮处重复两次'click', () => displayPrices(prices)
  • 是的,这更有意义,非常感谢!!
【解决方案2】:

导出时,您必须从要导出的源文件的“范围”执行此操作,而不是从当前的函数(即从exports() 函数内部)进行。

一个简单的解决方法是修改您的 JavaScript.js 模块,以便您想要导出(并导入到另一个模块)的变量在声明时直接导出,在您的 JavaScript.js 模块的顶层:

JavaScript.js

// Start of JavaScript.js file - do not nest exports inside of any function, etc

export const cakeChoicePricing = // Some value or function
export const cakeTypeResultPricing = // Some value or function

export const cupcakeChoicePricing = // ..
export const cupcakeTypeResultPricing = // ..

其他模块

// This can be merged into single import statement

import { 
    cakeChoicePricing, 
    cakeTypeResultPricing, 
    cupcakeChoicePricing, 
    cupcakeTypeResultPricing
} from './JavaScript.js';

【讨论】:

  • 感谢您的快速回复!也许我的问题不是很清楚,但我只想在代码的前面部分修改后并且仅在按下某个按钮时导出这些变量。我正在尝试创建一些订单系统,用户可以为蛋糕或纸杯蛋糕选择不同的选项,例如 Red Velvet 蛋糕,所以 cakeTypeResultPricing = 'Red Velvet' 并在另一个 HTML 页面上显示最终选项。我希望这能把事情弄清楚一点。谢谢。
  • only when a certain button is pushed 您不能有条件地导出 - 请重新考虑您的代码 - 如何导入尚未导出的内容?
猜你喜欢
  • 2022-08-14
  • 1970-01-01
  • 1970-01-01
  • 2019-06-13
  • 1970-01-01
  • 1970-01-01
  • 2020-12-17
  • 2017-07-03
  • 2019-06-13
相关资源
最近更新 更多