babel编译后提示import的值无法读取

点点乐淘淘
  • 330

目录结构是这样的
图片描述

上代码
profile.js

function showMsg(msg) {
    alert(msg);
}
export default {showMsg};

import.js

import {showMsg} from "./profile.js";

showMsg("hello world");

.babelrc

{
    "presets":[
        "es2015"
    ],
    "plugins":["transform-es2015-modules-umd"]
}

package.json

{
  "name": "module",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "babel src/import.js -o dist/index.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-cli": "^6.26.0",
    "babel-plugin-transform-es2015-modules-umd": "^6.24.1",
    "babel-preset-es2015": "^6.24.1"
  }
}

运行后提示 Cannot read property 'showMsg' of undefined

回复
阅读 1.7k
1 个回答

profile.js

function showMsg(msg) {

alert(msg);

}
export {showMsg};

删除 default

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏