zoukankan      html  css  js  c++  java
  • es6编写generator报错

    首先babel基础包(不安装额外东西)并不是支持完整的es6语言

    自己写的如下代码

    let generator = function* () {
            yield 1;
            yield * [2,3,4];
            yield 5;
            
        };
    
        var gen = generator();
        console.log('1',gen.next());
        console.log('2',gen.next());
        console.log('3',gen.next());

    报错

    "D:Program FilesJetBrainsWebStorm 2017.2.5in
    unnerw.exe" "D:Program Files
    odejs
    ode.exe" D:chengxuvuepro2distappclasssrclesson1.js
    D:chengxuvuepro2distappclasssrclesson1.js:34
    	var generator = /*#__PURE__*/regeneratorRuntime.mark(function generator() {
    	                             ^
    
    ReferenceError: regeneratorRuntime is not defined
        at Object.<anonymous> (D:chengxuvuepro2distappclasssrclesson1.js:34:31)
        at Module._compile (module.js:635:30)
        at Object.Module._extensions..js (module.js:646:10)
        at Module.load (module.js:554:32)
        at tryModuleLoad (module.js:497:12)
        at Function.Module._load (module.js:489:3)
        at Function.Module.runMain (module.js:676:10)
        at startup (bootstrap_node.js:187:16)
        at bootstrap_node.js:608:3
    
    Process finished with exit code 1
    

    自己的环境如下

    {
      "name": "pro2",
      "version": "1.0.0",
      "description": "",
      "main": "index.js",
      "scripts": {
        "test": "echo "Error: no test specified" && exit 1"
      },
      "keywords": [],
      "author": "",
      "license": "ISC",
      "devDependencies": {
        "babel-polyfill": "^6.26.0",
        "babel-preset-es2015": "^6.24.1"
      }
    }

    解决方法:在自己的项目js文件中最开头写上import "babel-polyfill"; 在运行,就可以成功运行。

    1 { value: 1, done: false }
    2 { value: 2, done: false }
    3 { value: 3, done: false }

      

  • 相关阅读:
    STM32位带操作
    url参数 加密
    object.key 对象的键排序 可能出现的问题
    在vue项目中 获取容器的高度
    navigator 判断移动端是Android还是iOS
    Nginx下载地址
    Sublime Text3快捷键大全
    鼠标拖着元素飞
    g6 cavans
    vue img标签图片加载时 闪烁
  • 原文地址:https://www.cnblogs.com/zhaopengcheng/p/8011816.html
Copyright © 2011-2022 走看看