【问题标题】:How to display data from function in component nativescript?如何在组件 nativescript 中显示来自函数的数据?
【发布时间】:2020-01-31 18:13:18
【问题描述】:

如何在组件中显示来自函数的数据?我的功能是:

getStrings() {
    let myString:any;
      http.getString("https://projectzenith.pl/testy/send.php").then((r:string) => {
         myString.set("getStringResult" ,r );
      },(e) =>{});
      return myString;
  }

我想以这样的方式显示myString

<StackLayout>
<Label text="My new string">
</Label>
<TextView text=["myString"]></TextView>
</StackLayout> 

但我看到错误:未定义标识符“myString”。

【问题讨论】:

    标签: nativescript


    【解决方案1】:

    您的getStrings 函数正在调用异步函数但立即返回,但不保证结果已首先存储在myString 中。您的模板引用的是函数局部变量而不是实例局部变量(或函数本身)。有多种方法可以解决这些问题。以下内容对您的源的影响最小,但很可能会出现同步错误。

      public myString: string;
    
      constructor() {
        this.getStrings();
      }
    
      getStrings() {
        http.getString("https://projectzenith.pl/testy/send.php").then((r: string) => this.myString = r);
      }
    
    <Label [text]="myString"></Label>
    

    【讨论】:

      猜你喜欢
      • 2020-10-16
      • 1970-01-01
      • 2019-12-14
      • 2019-12-30
      • 2021-06-28
      • 2018-04-14
      • 2018-02-18
      • 2018-12-06
      • 2020-01-31
      相关资源
      最近更新 更多