简体   繁体   English

ES6-如何在其他模块中修改变量

[英]ES6 - How to modify a variable in other modules

in module Global.js: 在模块Global.js中:

export let transLog = [];

in main: 在主要方面:

import * as G from "./Global";
G.transLog = [];

I got a error: 我收到一个错误:

app.js?c99e:19 Uncaught TypeError: Cannot set property q of #<Object> which has only a getter
    at eval (app.js?c99e:19)
    at Object.<anonymous> (bootstrap e92860b74eb6dd40b159:62)
    at __webpack_require__ (bootstrap e92860b74eb6dd40b159:19)
    at bootstrap e92860b74eb6dd40b159:62
    at bootstrap e92860b74eb6dd40b159:62

webpack config: webpack配置:

const webpack = require('webpack');

module.exports = {
    entry: './js/app.js',
    plugins: [
        new webpack.SourceMapDevToolPlugin({
            filename: "[file].map"
        })
    ],
    output: {
        filename: './dist/app.js'
    },
    devtool: 'source-map'
};

So, how to modify a variable in other modules? 那么,如何在其他模块中修改变量?

You cannot assign new values to exported variables, only the module itself can do that (and when it does, it can be pretty confusing, so I'd recommend to avoid this). 您不能为导出的变量分配新值,只有模块本身可以做到这一点(并且这样做确实会造成混乱,因此建议避免这种情况)。

You can mutate exported objects though, eg G.translog.push(…) or G.translog.length = 0 . 您可以G.translog.push(…)导出的对象,例如G.translog.push(…)G.translog.length = 0

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

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