首页 > 解决方案 > 如何创建可以跨模块使用的全局变量?

问题描述

我将场景从文件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

标签: javascript

解决方案


ChrisG建议的解决方案:导出对象并编辑其值

const globals = {
      camera: null,
      controls: null,
      renderer: null,
      labelRenderer: null,
      renderer3D: null,
      arrow: null,
      label: null,
      scene: null
    };
    
    export { globals };

推荐阅读