UI不能正常显示鸿蒙接口返回数据

davisl
发布于 2020-9-28 19:08
浏览
0收藏

当正常调用鸿蒙接口获取数据时,想用text来显示返回的数据,但是发现返回数据总是不显示

 

比如说:

js文件中点击click函数获取接口返回的数据

 

    data: {


        title: 'World',


        moduleGroup: null,


    },

    click: function() {

        this.title = "click";

        var type = "get";

        if (this.moduleGroup == null) {

            this.moduleGroup = ModuleGroup.getGroup("CurDemo/getValue");


        }

        if (this.moduleGroup != null) {

        this.title = "moduleGroup not empty";

            this.moduleGroup.callNative(type).then(function(value) {


                this.title = value;

            });

        }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.

 


    },

 


发现界面上显示了moduleGroup not empty,但是不显示value的值

 

 

这是因为js的闭包问题,解决方法如下:

 

    click: function() {
        this.title = "click";

        var type = "get";
        if (this.moduleGroup == null) 
            this.moduleGroup = ModuleGroup.getGroup("CurDemo/getValue");
        }
        if (this.moduleGroup != null) {
            this.title = "moduleGroup not empty";

            var self = this;
            this.moduleGroup.callNative(type).then(function(value) {
                self.title = value;
            });
        }

    },
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.

 

 

分类
2
收藏
回复
举报
2
2
2条回复
按时间正序
/
按时间倒序
小题大作
小题大作

楼主这个分享的太及时了

回复
2020-9-28 19:53:21
小蝌蚪
小蝌蚪

有问有答 你赞有了

回复
2020-9-28 20:07:22
回复
    相关推荐