简体   繁体   English

如何创建可以跨模块使用的全局变量?

[英]How do I create global variables I can use across my modules?

I import scene from file sceneSetup.js into initFloor1.js and get error ReferenceError: scene is not defined Can't find solution of this issue and can't understand if it is the issue of javascript or Threejs我将场景从文件sceneSetup.js导入initFloor1.js并得到错误 ReferenceError: scene is not defined找不到此问题的解决方案,无法理解是 javascript 还是 Threejs 的问题

sceneSetup.js:场景设置.js:

export let camera, controls, renderer, labelRenderer, renderer3D, arrow, label, scene;

initFloor1.js: 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();

My project folder:我的项目文件夹:

|- src
|  |- 3d-scene
|     |- sceneSetup.js
|     |- Floor1  
|        |- initFloor1.js

Solution, suggested by ChrisG : export an object and edit its values ChrisG建议的解决方案:导出 object 并编辑其值

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

Use window.variableName to declare in central place and then you can access across the site.使用window.variableName在中心位置声明,然后您可以跨站点访问。

window.camera=null;
window.controls=null;

etc.等等

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM