[英]Uncaught SyntaxError: Unexpected token { when importing an array
I'm doing an app for a class, and the professor is just as confused as I am about why it's not working. 我正在为一个班级准备一个应用程序,而教授却对为什么它不起作用感到困惑。 I have 2 js files and one html.
我有2个js文件和一个html。 I'm exporting an array from one js to the other, then using that js as the src in my html.
我正在将数组从一个js导出到另一个js,然后将该js用作html中的src。 Here's the pertinent code:
以下是相关代码:
medList.js: medList.js:
export const CompleteMedList = [
.....
];
main.js: main.js:
import {CompleteMedList} from './MedList.js'
html: HTML:
<script src="main.js"></script>
And here's the error I'm given: 这是我得到的错误:
main.js:1 Uncaught SyntaxError: Unexpected token {
main.js:1未捕获的SyntaxError:意外令牌{
I tried using type="module" in the script tag, but then it gives me this error when I try do do stuff using the functions declared in main.js: 我尝试在script标签中使用type =“ module”,但是当我尝试使用main.js中声明的函数执行操作时,它给了我这个错误:
Uncaught ReferenceError: openCycleForm is not defined at HTMLButtonElement.onclick
未捕获的ReferenceError:未在HTMLButtonElement.onclick上定义openCycleForm
Here's the function that the error references in main.js: 这是main.js中错误引用的函数:
function openCycleForm() {
document.getElementById("newCyclePg1").style.width = "100%";
for (var i=0; i < CompleteMedList.length; i++){
document.getElementById("fullMedsList").innerHTML +=
"<input type=\"checkbox\" name=\"meds\" id=\"" + CompleteMedList[i].id + "\">" +
CompleteMedList[i].name + "<br></br>";
}
}
I initially had the code in the html file (where it worked), but just moved it to main.js so I could use an array of data instead of hardcoding it, and then everything broke... 我最初将代码保存在html文件中(工作的地方),但只是将其移至main.js,因此我可以使用数据数组而不是对其进行硬编码,然后一切都崩溃了...
It looks like it's being a bit funny with the inline JS, so I suggest you remove it and add the event listeners in main.js
instead. 内联JS看起来有点有趣,所以我建议您删除它,然后在
main.js
添加事件侦听main.js
。 Something like this which I have working locally on my machine at the moment: 像这样的东西,我现在在我的机器上本地工作:
HTML HTML
<button type="button" class="currentCycle">Current Cycle</button>
JS JS
const currentCycleButton = document.querySelector('.currentCycle');
currentCycleButton.addEventListener('click', openCurrentCycle, false);
function openCurrentCycle() {
document.getElementById("currentCycle").style.width = '100%';
console.log('done');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.