为什么在导入变量之后变为常数?
执行index.js后, package.json
{
"type": "module"
}
users.js
let users = ["Jack", "Mary"];
export default users;
index.js
import users from './users.js';
users = [];
我遇到了一个错误:
users = [];
^
TypeError: Assignment to constant variable.
为什么? 用户
明确定义为变量而不是常数。
package.json
{
"type": "module"
}
users.js
let users = ["Jack", "Mary"];
export default users;
index.js
import users from './users.js';
users = [];
After executing index.js I get an error:
users = [];
^
TypeError: Assignment to constant variable.
Why? The users
was clearly defined as a variable not a constant.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
与
之后无法分配值。它不是完全相同的,因为值可以更改,但是只能从模块内部更改它们。因此,用户内部
index.js
is similar to
in that you cannot assign the value afterward. It's not quite the same because the values can change, but they can only be changed from inside the module. so inside users.js
index.js
正如@pilchard所解释的那样,要完成我的任务,我需要在模块内更改此数组。那是因为我导入的值仅在模块之外读取。 docs 。
users.js
index.js
As @pilchard explained, to accomplish my task I need to change this array inside the module. That's because values that I import are read-only outside the module. Docs.
users.js
index.js