昨天公司實習生問我這個
import
為什麼一直報錯,他代碼如下:
報錯資訊如下:
Uncaught SyntaxError: Cannot use import statement outside a module
複制
實際上是因為
script
标簽沒有加
type
導緻的
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<script type="module">
import {ruben} from './js/module.js'
console.log(ruben)
</script>
</body>
</html>
複制
這樣就可以了
可以看到正常輸出
當然,我們使用
export default
也是一樣的
let ruben = "module"
export default ruben
複制
在外部引用
<script type="module">
import ruben from './js/module.js'
console.log(ruben)
</script>
複制