请选择 进入手机版 | 继续访问电脑版
 找回密码
 立即注册
搜索

异步调用方法报错,提示“regeneratorRuntime is not defined”

1
回复
435
查看
[复制链接]

46

主题

47

帖子

465

积分

 楼主| 2021-6-4 15:04:58 显示全部楼层 |阅读模式

现象描述

使用异步调用await方法,客户端提示“regeneratorRuntime is not defined”,具体错误信息如下图所示:


回复

使用道具 举报

23

主题

142

帖子

825

积分

2021-6-4 15:08:04 显示全部楼层

问题分析

对于await的方式调用需要开发者引入@babel/runtime/regenerator,示例代码如下:

<script>
const injectRef = Object.getPrototypeOf(global) || global;
// injection regeneratorRuntime
injectRef.regeneratorRuntime = require('@babel/runtime/regenerator');
module.exports = {
}
</script>

解决方法

在app.ux文件中起始位置加入上述代码声明即可,具体代码如下:

  1. <script>

  2. import push from "@service.push";

  3. import device from '@system.device';

  4. const injectRef = Object.getPrototypeOf(global) || global;

  5. // injection regeneratorRuntime

  6. injectRef.regeneratorRuntime = require('@babel/runtime/regenerator');

  7. module.exports = {

  8. data: {

  9. hasSaved: "no"

  10. },

  11. onCreate() {

  12. console.info("Application onCreate");

  13. console.info("push服务提供商:" + push.getProvider());

  14. this.hasSaved = "yes";

  15. push.subscribe({

  16. success: function (data) {

  17. console.log("push.subscribe succeeded, regid=" + JSON.stringify(data));

  18. },

  19. fail: function (data, code) {

  20. console.log("push.subscribe failed, result data=" + JSON.stringify(data) + ", code=" + code);

  21. },

  22. complete: function () {

  23. console.log("push.subscribe completed");

  24. }

  25. });

  26. this.testAsync();

  27. },

  28. async testAsync() {

  29. try {

  30. let res = await device.getDeviceId();

  31. console.info("get device id " + JSON.stringify(res.data));

  32. } catch (e) {

  33. console.log("get device id error " + e.code);

  34. }

  35. },

  36. onShow(options) {

  37. //console.info("manifest全局数据2:" + this.$app.$data.baseUrl);

  38. },

  39. onDestroy() {

  40. console.info("Application onDestroy");

  41. },

  42. dataApp: {

  43. localeData: {}

  44. }

  45. };

  46. </script>


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册