首页 新闻 会员 周边

es6 module import的疑惑

0
悬赏园豆:10 [待解决问题]

calculator.js
const name = 'xiaosuan';
const add = function (a,b) { return a + b; }
export { name, add }

index.js
import {add} from ‘./calculator.js’
function addFunc(a, b) {
return add(a,b);
}
console.log(addFunc(100, 200));

index.html
<script type="module" src="~/js/index.js"></script>
<script>
var result = addFunc(100,200);
console.log(result );
</script>

在浏览index.html时提示addFunc不存在,这是为什么?是ES6不支持这样的语法吗?我是想新用个JS把第三方模块的方法封装后对外使用,html使用的时候直接引用,不用在import,这样可以吗?

HNLY的主页 HNLY | 初学一级 | 园豆:95
提问于:2022-06-22 16:29
< >
分享
所有回答(1)
0

在浏览index.html时提示addFunc不存在,这是为什么?

没有导出,index.js需要导出addFunc才行

index.js
import {add} from ‘./calculator.js’
function addFunc(a, b) {
return add(a,b);
}
console.log(addFunc(100, 200));

export { addFunc }
Laggage | 园豆:878 (小虾三级) | 2022-06-23 11:35
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册