es7如何跑起來裝飾器
阿新 • • 發佈:2020-08-23
今天學習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環境下執行