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 }

      

  • 相关阅读:
    node.js结合wechaty实现微信机器人[基础篇]
    .env文件为NodeJS全局环境变量
    基于jquery实现一个提示插件
    Puppeteer实现一个超简单的自动化机器人
    Vue高仿阿里动态banner,制作组件
    css不常用属性
    Vue表单校验失败滚动到错误位置
    C# Func委托
    深入解析C# 4th 笔记(第一章)
    C# 笔记 XML基础
  • 原文地址:https://www.cnblogs.com/zhaopengcheng/p/8011816.html
Copyright © 2011-2022 走看看