简体   繁体   English

Uncaught SyntaxError:导入数组时出现意外的标记{

[英]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.

相关问题 在 TypeScript 中导入时出现“Uncaught SyntaxError: Unexpected token {” - "Uncaught SyntaxError: Unexpected token {" when importing in TypeScript 当没有“:”时,“未捕获的SyntaxError:意外令牌:”吗? - “Uncaught SyntaxError: Unexpected token :” when there is no “:”? 导入时出现“ Uncaught SyntaxError:意外的令牌&lt;”错误 - “Uncaught SyntaxError: Unexpected token <” error while importing “未捕获的SyntaxError:导入时出现意外的令牌{” - “Uncaught SyntaxError: Unexpected token {” while importing 未捕获到的SyntaxError:意外的令牌:返回JSON时 - Uncaught SyntaxError: Unexpected token : when returning JSON Uncaught SyntaxError:使用本地主机时出现意外令牌 - Uncaught SyntaxError: Unexpected token when using localhost 调用函数时出现未捕获的SyntaxError:意外令牌) - Uncaught SyntaxError: Unexpected token ) when calling a function 未捕获到的SyntaxError:意外令牌。 使用ClassNotty时 - Uncaught SyntaxError: Unexpected token . when using ClassNotty 解析jsonp时出现“未捕获的SyntaxError:意外令牌” - “Uncaught SyntaxError: Unexpected token” when parse jsonp 加载模块时出现“未捕获到的SyntaxError:意外令牌{” - “Uncaught SyntaxError: Unexpected token {” when loading module
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM