欢迎来到 跌倒的小黄瓜 的博客

♪(^∇^*)我要当大佬,(#^.^#)哈哈哈哈,(。-ω-)zzz我要成为优秀的人,(*^▽^*)٩(๑>◡<๑)۶O(∩_∩)O哈哈~~~~~~~~欢迎━(*`∀´*)ノ亻!

es7如何跑起来装饰器

今天学习es7新特性装饰器时,代码提示语法错误,babel照着以前的方法转码也不成功,故写下此文谈谈如何解决

大致步骤如下:
1.安装babel转码工具
2.安装第三方插件,用于支持decorators
3.配置jsconfig.json解决vscode提示语法错误
4.babel打包成功运行

注:我这里没有配置.babelrc文件

1,2.npm install babel-cli babel-plugin-transform-decorators-legacy babel-register --save-dev

3.jsconfig.json

{
    "compilerOptions": {
        "experimentalDecorators": true
    }
}

4.package.json

{
  "name": "test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "babel --plugins transform-decorators-legacy src -d lib"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-plugin-transform-decorators-legacy": "^1.3.5",
    "babel-register": "^6.26.0"
  },
  "dependencies": {
    "babel-cli": "^6.26.0"
  }
}

最后执行打包命令:npm run build,即可成功打包代码,并且可在node环境下运行

运行也可使用require,不过还是建议使用上面那种

require('babel-register')({
    plugins: ['transform-decorators-legacy']
});
require("./input.js")
posted @ 2020-08-23 10:00  跌倒的小黄瓜  阅读(197)  评论(0编辑  收藏  举报