语法错误:导入声明只能出现在模块的顶层

问题描述 投票:0回答:1

我正在尝试从'index.js'中的'test.js'导入函数

test.js:

var testFunction = function test(){ alert("hello World") }

export testFunction

在“ index.js”的最顶部,我尝试了以下语句:

import testFunction from './test.js'

现在,当我在浏览器中运行index.html文件时,我的firefox控制台出现以下错误:

SyntaxError:导入声明只能出现在模块的顶层(在第1行中)

javascript import syntax export
1个回答
0
投票

解决方案:

test.js

var testFunction = function test(){ alert("hello World") }

export {testFunction}


index.js:
import testFunction from './test.js'

//...

的HTML:
<script type="module" src="./index.js">

© www.soinside.com 2019 - 2024. All rights reserved.