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

Lix*_*Wei 2 javascript ecmascript-6 es6-modules

在 Global.js 模块中:

export let transLog = [];
Run Code Online (Sandbox Code Playgroud)

主要内容:

import * as G from "./Global";
G.transLog = [];
Run Code Online (Sandbox Code Playgroud)

我收到一个错误:

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
Run Code Online (Sandbox Code Playgroud)

网络包配置:

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'
};
Run Code Online (Sandbox Code Playgroud)

那么,如何修改其他模块中的变量呢?

Ber*_*rgi 6

您不能为导出的变量分配新值,只有模块本身可以做到这一点(当它这样做时,它可能会非常混乱,所以我建议避免这种情况)。

\n\n

不过,您可以改变导出的对象,例如G.translog.push(\xe2\x80\xa6)G.translog.length = 0

\n