async/await __generator 未定义
Posted
技术标签:
【中文标题】async/await __generator 未定义【英文标题】:async/await __generator is not defined 【发布时间】:2017-06-09 08:46:50 【问题描述】:我正在尝试将 async/await 函数与 angular2-webpack-starer 和 typescript 一起使用(现在支持针对 es5 的此函数),但出现错误:
这是组件内的代码:
// function inside component
async checkSlug(slug: string)
// nothing here
我正在使用 webpack2.2 和 typescript 2.1.5。这是我的 tsconfig:
"compilerOptions":
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"sourceMap": true,
"noEmit": true,
"noEmitHelpers": true,
"strictNullChecks": false,
"baseUrl": "./src",
"paths":
,
"lib": [
"dom",
"es7",
"es2015.promise"
],
"types": [
"hammerjs",
"jasmine",
"node",
"protractor",
"selenium-webdriver",
"source-map",
"uglify-js",
"webpack",
"chai",
"chai-as-promised",
"lodash"
]
,
"exclude": [
"node_modules",
"dist"
],
"awesomeTypescriptLoaderOptions":
"forkChecker": true,
"useWebpackText": true
,
"compileOnSave": false,
"buildOnSave": false,
"atom": "rewriteTsconfig": false
【问题讨论】:
您已经告诉编译器不要使用"noEmitHelpers": true
为您生成该辅助函数。如果您将其设置为 false 它应该可以工作。否则你需要提供辅助函数。见blog.mariusschulz.com/2016/12/16/…
【参考方案1】:
好的,这个 tsconfig.json 有效
"compilerOptions":
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"sourceMap": true,
"noEmit": true,
"noEmitHelpers": false,
"strictNullChecks": false,
"baseUrl": "./src",
"paths":
,
"lib": [
"dom",
"es7",
"es5",
"es2015.promise"
],
"types": [
"hammerjs",
"jasmine",
"node",
"protractor",
"selenium-webdriver",
"source-map",
"uglify-js",
"webpack",
"chai",
"chai-as-promised",
"lodash"
]
,
"exclude": [
"node_modules",
"dist"
],
"awesomeTypescriptLoaderOptions":
"forkChecker": true,
"useWebpackText": true
,
"compileOnSave": false,
"buildOnSave": false,
"atom": "rewriteTsconfig": false
【讨论】:
这个答案对解决方案比较清楚:***.com/a/42426996/1569600noEmitHelpers: false
以上是关于async/await __generator 未定义的主要内容,如果未能解决你的问题,请参考以下文章
async(await)函数和 Generator 函数 区别
Generator与async/await与Generator的模拟
进阶学习7:JavaScript异步编程——Generator异步方案Async/ Await