【问题标题】:How to convert ES6 code to ES5 code on Run Time of Angular App如何在 Angular App 的运行时将 ES6 代码转换为 ES5 代码
【发布时间】:2019-07-19 11:30:26
【问题描述】:

我从 API 获得了一个角度模块的详细信息。该模块在 ES6 中 我想将该模块代码转换为 ES5 语法。

API Used for Module Response

我在安装 babel 运行时后尝试了import { transform } from 'babel-core';。但它正在抛出错误。

load() {
    fetch(url) // making the get request for loading the module response
      .then(response => response.text())
      .then(source => {
// i want to convert this response to es5 standard. right now it is in es6
      });
  }

我的期望是在 Angular 运行时将 es6 转换为 es5 代码 组件函数。

【问题讨论】:

  • 为什么?如果您正确配置,打字稿编译器会为您执行此操作。见typescript configuration docs
  • @Liam 实际上响应是字符串形式的。我想转换那个字符串代码。对此有何建议?
  • “以字符串形式”是什么意思?
  • 等待加载外部库然后运行它?这听起来像是等待发生的代码注入攻击

标签: javascript angular ecmascript-5 es5-compatiblity


【解决方案1】:

做:

npm install --save-dev @babel/core @babel/preset-env

然后:

import * as babel  from '@babel/core';

load() {
    fetch(url)
      .then(response => response.text())
      .then((source) => {
         return babel.transform(source, {
           presets: ['@babel/preset-env']
         });
      });
  }

【讨论】:

    猜你喜欢
    • 2016-05-03
    • 1970-01-01
    • 2017-01-07
    • 2020-06-07
    • 1970-01-01
    • 1970-01-01
    • 2016-08-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多