javascript - 如何创建可以跨模块使用的全局变量?
问题描述
我将场景从文件sceneSetup.js
导入initFloor1.js
并得到错误 ReferenceError: scene is not defined
Can't find solution of this issue and can't understand if it is issue of javascript or Threejs
场景设置.js:
export let camera, controls, renderer, labelRenderer, renderer3D, arrow, label, scene;
initFloor1.js:
import * as THREE from 'three';
import { camera, controls, renderer, labelRenderer, renderer3D, arrow, label, scene } from './../sceneSetup.js'
export function initFloor1() {
scene = new THREE.Scene();
我的项目文件夹:
|- src
| |- 3d-scene
| |- sceneSetup.js
| |- Floor1
| |- initFloor1.js
解决方案
ChrisG建议的解决方案:导出对象并编辑其值
const globals = {
camera: null,
controls: null,
renderer: null,
labelRenderer: null,
renderer3D: null,
arrow: null,
label: null,
scene: null
};
export { globals };
推荐阅读
- graph-theory - 在无向无环图中找到最长回文路径的长度
- string - 如何在 Powershell 中获取字符串的特定部分
- linux - 如何将 Ctrl-C 键发送到正在运行的应用程序而不是禁用 tmux 弹出窗口?
- flutter - 在 Flutter 的本地通知中添加按钮
- node.js - 无法将另一个集合中的对象插入到表单提交时创建的新文档
- laravel - Laravel - 如何更新动态输入字段
- 8thwall-xr - 如何在 Mac 中为 8thwall web ar 编写代码并进行测试?
- python - 如何使用不同的功能来替换列表中的项目?
- c# - 在子视图 XAML 中使用来自父视图的命令
- node.js - Nodejs 是否有任何支持创建 winform 的框架/库?