zoukankan      html  css  js  c++  java
  • 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")
    
  • 相关阅读:
    DELPHI中GetTickCount 函数的应用
    Delphi 中 StrToIntDef 函数的应用
    TeeChart 动态建曲线
    历遍指定文件夹下的文件
    DELPHI 获取错误信息
    delphi播放flash
    C# 获取当前路径方法
    DELPHI 修改系统分辨率
    SQL字符串处理函数 Yang
    Linux基本操作 Yang
  • 原文地址:https://www.cnblogs.com/ygjzs/p/13548363.html
Copyright © 2011-2022 走看看