private app; obj: RootObject ; constructor(private _http: HttpClient) { this.app = new Clarifai.App({ ApiKey: "CENSOR BAR" }); }; public getColorValues(imageUrl: string): RootObject { this.app.models.predict('eeed0b6733a644cea07cf4c60f87ebb7', imageUrl).then( function (response) { this.obj = response; }, function (error) { this.obj = "There was an error"; } ); let i: number; while (this.obj == null) { i += 1; } console.log("Waited " + i + " cycles for response.") console.log("Object: " + this.obj); return this.obj; }
async ,但您正在将其作为同步调用处理。他回来了 this.obj
async
this.obj
除此之外,它根本不会被设置,因为您正在使用 function 关键字更改 this 对本地函数的引用
function
this
getColorValues Promise<RootObject> :
getColorValues
Promise<RootObject>
getColorValues(imageUrl: string): Promise<RootObject> { return this.app.models.predict('eeed0b6733a644cea07cf4c60f87ebb7', imageUrl); }
就这样,这就是你所需要的。当你打电话给 获取颜色值
获取颜色值
getColorValues(imageUrl).then((resp) => { this.obj = resp; // this.obj contains what you want });