Lixin Wei
Lixin Wei

Reputation: 533

ES6 - How to modify a variable in other modules

in module 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:

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?

Upvotes: 2

Views: 2234

Answers (1)

Bergi
Bergi

Reputation: 664356

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, e.g. G.translog.push(…) or G.translog.length = 0.

Upvotes: 7

Related Questions