如何在 JS 中导出数组?
How can I export array in JS?
我正在尝试在我的 javascript chrome 扩展中导出和导入一个数组,但它给了我 Uncaught SyntaxError: Unexpected token 'export'错误。
这是我的代码:
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');
您需要在调用脚本的 HTML <script>
标记中添加 type="module"
。
您还可以大大简化您的代码,这是值得的。
let urls = [];
for (const link in Document.links) {
if (link.hostname === location.hostname) urls.push(link.href)
}
export default urls
我正在尝试在我的 javascript chrome 扩展中导出和导入一个数组,但它给了我 Uncaught SyntaxError: Unexpected token 'export'错误。
这是我的代码:
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');
您需要在调用脚本的 HTML <script>
标记中添加 type="module"
。
您还可以大大简化您的代码,这是值得的。
let urls = [];
for (const link in Document.links) {
if (link.hostname === location.hostname) urls.push(link.href)
}
export default urls