如何在组件中显示来自函数的数据?我的功能是:
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“。
发布于 2020-02-01 02:37:02
您的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>https://stackoverflow.com/questions/60009222
复制相似问题