xx *_* yy 7 javascript node.js ecmascript-6 es6-module-loader es6-modules
我有两个文件,file1 导出一个变量“不是常量” var x=1 和 file2 从中导入这个变量
问题是我无法修改导入的变量,即使它不是常量!
文件1.js
export var x=1 //it is defined as a variable not a constant
Run Code Online (Sandbox Code Playgroud)
文件2.js
import {x} from 'file1.js'
console.log(x) //1
x=2 //Error: Assignment to constant variable
Run Code Online (Sandbox Code Playgroud)
这是不可变的导出模块值的影响。您可以使用同一模块中的另一个函数覆盖它
在您的文件 1 中:
export let x = 1;
export function modifyX( value ) { x = value; }
Run Code Online (Sandbox Code Playgroud)
在你的文件 2
import { x, modifyX } from "file1.js"
console.log( x ) // 1;
modifyX( 2 );
console.log( x ) // 2;
Run Code Online (Sandbox Code Playgroud)