javascript - 通过 eval 访问模块变量时,Edge 抛出未定义的错误
问题描述
我无法在片段中模仿这一点,但使用以下代码重现:
Windows 10 || 边缘版本:42.17134.1.0 || EdgeHTML:17.17134
我正在使用ES6 模块
import * as variable_name from 'file'
console.log(variable_name); //will return an [object module]
console.log(eval("variable_name")); //returns undefined error 'variable_name is not defined'
Chrome 以这种方式工作正常。
示例 HTML
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<script type="module" src="module.js"></script>
</body>
</html>
解决方案
您定义variable_name
为变量值,因此您应该在不带引号的情况下使用它。也许 Chrome 中的语法规则不是那么严格,所以它可以很好地使用引号。您可以将其修改为console.log(eval(variable_name));
,然后它可以在 Edge 和 Chrome 中正常工作。
推荐阅读
- haskell - 如何在另一个函数的结果列表上使用 map ?
- java - Mapstruct - 从多个集合映射到单个集合
- php - 如何在wordpress中查找帖子类别源文件
- dart - 如何使用 android_intent 通过颤振打开位置设置
- spring-mvc - Spring Boot @RequestParameter 意外行为
- angular - 锚点滚动仅适用于 Angular 6 中的第二次点击
- php - PHP preg_split 将分隔符保留在不同的元素中
- qt - 是否可以在 QAxObject 中链接嵌套命令?
- spring-batch - 如何使用 java config 获取 MultiResourceItemReader 的当前资源
- java - 解析后对 Groovy 扁平化的 JsonSluper 对象进行排序