简体   繁体   English

从一个ts到另一个ts的角返回值

[英]Angular return value from one ts to another

In my angular project, I have two ts files as below: 在我的角度项目中,我有两个ts文件,如下所示:

Data.ts 数据

import { Injectable } from '@angular/core';
import { MyStorage } from '../storage';

@Injectable()
export class itemFromStorage {
constructor(public storage: Storage) {  

}

    GetItemDataFromStorage(current){        
        this.storage.get(current).then((val) => {
          return val;  //????    
        }); 
   };    

}

Main.ts 维护

import { itemFromStorage } from '../data'

export class item_thumb {
    constructor(public itemStorage: itemFromStorage) { 

    };  

   fetchThumbData(){
     var data = this.itemStorage.GetItemDataFromStorage(this.dataNumber);
     console.log(data);//????
   }
}

From the data.ts , how do I return the data value to main.ts ? data.ts ,如何将数据值返回到main.ts

your data.ts method should return only the request itself, without subscribing to it: 您的data.ts方法应仅返回请求本身,而不订阅该请求:

    GetItemDataFromStorage(current){        
        return this.storage.get(current);
   }; 

and in your main.ts method subscribe and receive the data: 并在您的main.ts方法中订阅并接收数据:

   fetchThumbData(){
     var data = this.itemStorage.GetItemDataFromStorage(this.dataNumber).then((data) => {
          console.log(data);//???? 
        }); 

   }

Most likely your Data.ts is returning a promise. 您的Data.ts很可能正在返回承诺。
Main.ts 维护

export class item_thumb {
    constructor(public itemStorage: itemFromStorage) { 
    };  
   fetchThumbData(){
     let promise = this.itemStorage.GetItemDataFromStorage(this.dataNumber);
     promise.then(data => console.log(data));     
   }
}

To get the value synchronously: Get the value of a Javascript Promise in a synchronous way 同步获取值: 以同步方式获取Javascript Promise的值

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular 5:如何显示 .ts 的返回值以供查看 - Angular 5: how to show return value from .ts to view 在 Angular 上的另一个组件(between.ts 文件)中使用一个变量 - Use a variable from one component in another (between .ts files) on Angular 从多个变量 angular/ts 中检查一个值是否为假 - check if one value is false from multiple variables angular/ts 从角度为6的service.ts返回undefined - return undefined from service.ts in angular 6 Angular:如何从一个组件的前端(app.compont.html)获取值到另一个组件的后端(other.component.ts) - Angular: How to get value from one component's frontend (app.compont.html) to another component's backend (other.component.ts) 如何在component.ts中将数据从一个函数传递到另一个函数,angular2 - how to pass data from one function to another in component.ts , angular2 如何在一个 angular 组件中从另一个组件调用 function(不同的.ts 文件) - How to call a function in one angular component from another component(different .ts file) 如何在angular/ts中将选定的数据从一页传输到另一页? - How to transfer selected data from one page to another page in angular/ts? 如何在角度7中将预订结果从一种方法返回到另一种方法? - How to return a subscription result from one method to another in angular 7? 如何在Angular 2中将值从一个字段解析并填充到另一个字段 - How to parse and populate the value from one field into an another in Angular 2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM