代码之家  ›  专栏  ›  技术社区  ›  JackTheKnife

javascriptWebpack-如何在模块之间共享变量?

  •  0
  • JackTheKnife  · 技术社区  · 6 年前

    在Webpack构建之后,我有javascript代码。

    代码 bundle.js 看起来像:

    /******/ (function(modules) { // webpackBootstrap
    /******/    // The module cache
    /******/    var installedModules = {};
    
    /******/    // The require function
    /******/    function __webpack_require__(moduleId) {
    
    /******/        // Check if module is in cache
    /******/        if(installedModules[moduleId])
    /******/            return installedModules[moduleId].exports;
    
    /******/        // Create a new module (and put it into the cache)
    /******/        var module = installedModules[moduleId] = {
    /******/            exports: {},
    /******/            id: moduleId,
    /******/            loaded: false
    /******/        };
    
    /******/        // Execute the module function
    /******/        modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
    
    /******/        // Flag the module as loaded
    /******/        module.loaded = true;
    
    /******/        // Return the exports of the module
    /******/        return module.exports;
    /******/    }
    
    
    /******/    // expose the modules object (__webpack_modules__)
    /******/    __webpack_require__.m = modules;
    
    /******/    // expose the module cache
    /******/    __webpack_require__.c = installedModules;
    
    /******/    // __webpack_public_path__
    /******/    __webpack_require__.p = "rainloop/v/0.0.0/static/js/min/";
    
    /******/    // Load entry module and return exports
    /******/    return __webpack_require__(0);
    /******/ })
    /************************************************************************/
    /******/ ([
    /* 0 */
    /*!********************!*\
      !*** ./dev/bundle.js ***!
      \********************/
    /***/ function(module, exports, __webpack_require__) {
    
        __webpack_require__(/*! bootstrap */ 74)(__webpack_require__(/*! App/User */ 6));
    
    /***/ },
    /* 1 */
    /*!*************************!*\
      !*** ./dev/Common/Utils.js ***!
      \*************************/
    /***/ function(module, exports, __webpack_require__) {
    
    
        (function () {
    
         .... code for Utils.js module ...
            module.exports = Utils;
    
        }());
    
    /***/ },
    /* 2 */
    /*!***************************!*\
      !*** external "window._" ***!
      \***************************/
    /***/ function(module, exports, __webpack_require__) {
    
        module.exports = window._;
    
    /***/ },
    /* 6 */
    /*!*************************!*\
      !*** ./dev/App/User.js ***!
      \*************************/
    /***/ function(module, exports, __webpack_require__) {
    
    
        (function () {
    
          ... code for App/User.js module ....
    
            module.exports = new AppUser();
    
        }());
    
    /***/ },
    

    等等。

    然后我试图申报

    var myVar;

    Common/Utils 我要导入的模块 App/User 模块AS

    Utils = __webpack_require__(/*! Common/Utils */ 1)

    但访问它并更新为 Utils.myVar 不起作用,也不将其声明为 window.myVar 通过 window 模块以上。

    我应该怎么做才能在模块之间共享这个变量?

    2 回复  |  直到 6 年前
        1
  •  0
  •   Oleksandr Martyniuk    6 年前

    var myVal = false;
    
    // more stuff here
    
    module.exports = { myVal, /* more stuff here */ };
    

    module.exports.myVal var myVal

    var config = { myVal: false };
    
    // more stuff here
    
    module.exports = { config, /* more stuff here */ };
    

    Utils.config var config

        2
  •  0
  •   JackTheKnife    6 年前

    (function() {window.RL.myVal = new ko.subscribable(); })(window.RL = window.RL || {});