导出嵌套变量

发布于 2025-02-05 09:57:48 字数 288 浏览 3 评论 0原文

当我尝试解释时,请忍受我。一旦从嵌套变量满足IF语句后,我想导出一个值,然后将其导入另一个.ts文件。

例子。

var create = {

if(condition == true){

 var x = 3;

export x;

    }

else return;

}

然后完成

import { x } from "./List"

if(x == 3){

make condition here true.

}

Please bear with me as I try to explain. I would like to export a value once the if statement is satisfied from a nested variable, then import this into another .ts file.

Example.

var create = {

if(condition == true){

 var x = 3;

export x;

    }

else return;

}

then when its done

import { x } from "./List"

if(x == 3){

make condition here true.

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

向地狱狂奔 2025-02-12 09:57:48

这里有几件事要注意:

  1. 出口必须是最高级别,您不能从功能或分支内部导出某些内容。
  2. 您可以修改导出变量,但只有从1个模块内部
  3. 从1开始。因此,模块不能根据条件具有可变数量的导出。您总是导出相同数量的元素,但其中一些元素可能不确定。

考虑到这一点,您可能需要的是设置一个变量,该变量将根据您的条件设置为不同的值,然后始终导出它。这样的事情:

let x = null; //maybe undefined, or another default value

if(condition == true){
 x = 3;
}

export x;

There are a couple of things to note here:

  1. exports need to be top level, you can't export something from inside a function or a branch.
  2. you can modify an exported variable, but only from inside the module
  3. from 1. it follows that a module cannot have a variable number of exports based on a condition. You always export the same number of elements, but some of them may be undefined.

Taking this into account, what you probable need is to set a variable that will be set to different values depending on your conditions and then always export it. Something like this:

let x = null; //maybe undefined, or another default value

if(condition == true){
 x = 3;
}

export x;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文