ES6 - 如何修改其他模块中的变量
ES6 - How to modify a variable in other modules
在模块 Global.js 中:
export let transLog = [];
主要内容:
import * as G from "./Global";
G.transLog = [];
我收到一个错误:
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 配置:
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'
};
那么,如何修改其他模块中的变量呢?
您不能为导出的变量分配新值,只有模块本身可以这样做(当它这样做时,它可能会非常混乱,所以我建议避免这种情况)。
不过您可以改变导出的对象,例如G.translog.push(…)
或 G.translog.length = 0
.
在模块 Global.js 中:
export let transLog = [];
主要内容:
import * as G from "./Global";
G.transLog = [];
我收到一个错误:
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 配置:
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'
};
那么,如何修改其他模块中的变量呢?
您不能为导出的变量分配新值,只有模块本身可以这样做(当它这样做时,它可能会非常混乱,所以我建议避免这种情况)。
不过您可以改变导出的对象,例如G.translog.push(…)
或 G.translog.length = 0
.