javascript - How can I export array in JS?
问题描述
I'm trying to export and import an array in my javascript chrome extention but it gives me Uncaught SyntaxError: Unexpected token 'export' error.
This is my code:
script.js
const urls = [];
for(var i = document.links.length; i --> 0;)
if(document.links[i].hostname === location.hostname)
urls.push(document.links[i].href);
export default urls;
urls.js
import urls from './script.js';
var element = document.querySelector('p');
element.textContent = urls.join('\n');
解决方案
You need to put type="module"
in the HTML <script>
tag calling your script.
You could also simplify your code quite a bit, for what it's worth.
let urls = [];
for (const link in Document.links) {
if (link.hostname === location.hostname) urls.push(link.href)
}
export default urls
推荐阅读
- c# - 输入字符串不正确 fromat:TimeSpan DisplayFormat
- regex - 缺少元素的正则表达式匹配排列
- c++ - 在 C++ 中是什么意思“它不拥有底层数据,因此复制或分配很便宜”
- ios - 尝试使用 React Native 广播 iBeacon
- python - 在附加时保留数据框列顺序
- pyspark - 插入缺失的日期行并在新行中插入旧值 PySpark
- swift - 如何让计时器跑得快于 1 秒
- c - 是否可以使用泛型函数将数组复制到 c 中另一个数组的不同类型
- php - MySQL 中的多个 Where 语句
- c# - C#如何将用户输入传递给where子句中的参数