requirejs - 如何从所需模块中的 main.js 文件中访问变量?
问题描述
我开始使用 Electron,我想知道如何从 main.js 文件头部的必需脚本中访问存在于我的 main.js 脚本中的变量。
这是我的愚蠢示例:
主.js:
const Electron = require('electron');
const App = Electron.app;
const menus = require('./assets/js/menus.js'); //this is the required script!
let myVar = false;
App.on('ready', function({
myVar = true;
})
//trying with module.exports
module.exports = {
myVar
}
菜单.js:
const Path = require('path');
const main = require(Path.resolve('./main.js'));
Main: [{
label:'mainLabel',
submenu:[{
label:'subLabel',
click:()=>{
console.log(**main.myVar**)//should be true when i access it, its undefined like it doesn't exists:(
}
}]
}]
我设法用 main.js 中的 getter 和 setter 做我想做的事,但我觉得这不是最好的方法。
有什么想法吗?有没有办法包含一个脚本,以便它可以访问脚本中声明的所有变量和函数,包括它?
解决方案
推荐阅读
- reactjs - 如何模拟传递给 react-final-form 的方法
- c# - 在实体框架中处理大数据的最佳方法
- optimization - .lp 文件的大小或 CBC/GLPK 中的变量数量是否有限制?
- javascript - 在 AngularJS 1.7.x 中登录后的 ngRoute 选项卡菜单
- hyperledger-fabric - Hyperledger Fabric 部署新的链码
- mysql - 选择记录时MySql更改日期
- apache - Apache 服务器:根据 Tomcat 中部署的应用程序直接连接到特定的工作人员
- php - 移动蛋糕php网站
- asp.net - 如何从 Internet Explorer 中删除尾部斜杠?
- mysql - MySQL - 用一个查询增加几行的值