浏览器:标识符X已经声明 [英] Browser: Identifier X has already been declared

查看:181
本文介绍了浏览器:标识符X已经声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在项目中使用带有Babel的ES6,当我声明我的 const

I am using ES6 with Babel in my project and I am getting an error when I declare one of my const

'use strict';

const APP = window.APP = window.APP || {};
const _ = window._;

APP.personalCard = (function () {

   ...

}());

错误


未捕获的TypeError:标识符APP已经被声明

Uncaught TypeError: Identifier 'APP' has already been declared

这是整个文件,我没有声明该文件中的任何其他位置。但是我已经在其他文件的顶部声明了var。

and that is the whole file, I don't have that declare anywhere else in that file. But I have declared that var in the top of the other files.

你认为它应该是什么?

推荐答案


但是我已经在其他文件的顶部声明了var。

But I have declared that var in the top of the other files.

这就是问题所在。毕竟,这会在同一个(全局)范围内对同一个名称进行多次声明 - 这会在 const 中引发错误。

That's the problem. After all, this makes multiple declarations for the same name in the same (global) scope - which will throw an error with const.

相反,使用 var ,在主文件中只使用一个声明,或者只分配给 window.APP 独家。

或者立即使用ES6模块,让你的模块捆绑器/加载器处理按预期暴露它们。

Instead, use var, use only one declaration in your main file, or only assign to window.APP exclusively.
Or use ES6 modules right away, and let your module bundler/loader deal with exposing them as expected.

这篇关于浏览器:标识符X已经声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆