Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 角度等待值,然后返回可观察值_Angular_Typescript_Asynchronous_Observable - Fatal编程技术网

Angular 角度等待值,然后返回可观察值

Angular 角度等待值,然后返回可观察值,angular,typescript,asynchronous,observable,Angular,Typescript,Asynchronous,Observable,我是个新手。。这是我的第一个项目,我注意到我的函数doSomething()在正确计算之前返回一个值。我查找了异步函数,但不知道如何在我的示例中实现它?如果有人明白这一点,请告诉我 这是我的代码: import { Injectable } from '@angular/core'; import { HttpClient, HttpHeaders } from '@angular/common/http'; import { Observable, of, TimeoutError } fro

我是个新手。。这是我的第一个项目,我注意到我的函数
doSomething()
在正确计算之前返回一个值。我查找了异步函数,但不知道如何在我的示例中实现它?如果有人明白这一点,请告诉我

这是我的代码:

import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable, of, TimeoutError } from 'rxjs';
import { getLocaleDateFormat } from '@angular/common';

@Injectable({
  providedIn: 'root'
})
export class CSVService {

  csvPath = "podaci.csv";
  userList: any[] = [];
  userData: CSVRecord[];
  temp = {name:'', last_name:'', postal_code:'', city:'', phone:''};

  constructor(private http: HttpClient) { }

  doSomething(): Observable<CSVRecord[]> {
    let csvList = this.getData();
    return of(csvList);
  }

  getUserData() {
    const headers = new HttpHeaders({'Content-Type':'application/json; charset=utf-8'});
    return this.http.get(this.csvPath, {responseType: 'text'});
  }

  getData(): CSVRecord[] {
    this.userData = [];
    this.getUserData().subscribe(data => {
      data = "\uFEFF"+data
      console.log(data);
      const list = data.split('\n');
        list.forEach( e => {
          const property = e.split(';');
          this.temp = {name:'', last_name:'', postal_code:'', city:'', phone:''};

          this.temp.name = property[0];
          this.temp.last_name = property[1];
          this.temp.postal_code = property[2];
          this.temp.city = property[3];
          this.temp.phone = property[4];

          this.userData.push(this.temp);
        }); 
      }); 
      return this.userData;
  }

}

export interface CSVRecord {
  name: string;
  last_name: string;
  postal_code: string;
  city: string;
  phone: string;
}
从'@angular/core'导入{Injectable};
从'@angular/common/http'导入{HttpClient,HttpHeaders};
从“rxjs”导入{observatable,of,TimeoutError};
从“@angular/common”导入{getLocaleDataFormat};
@注射的({
providedIn:'根'
})
导出类CSVService{
csvPath=“podaci.csv”;
用户列表:任意[]=[];
用户数据:CSVRecord[];
临时={姓名:'',姓氏:'',邮政编码:'',城市:'',电话:'};
构造函数(私有http:HttpClient){}
doSomething():可观察{
设csvList=this.getData();
返回(csvList);
}
getUserData(){
constheaders=newhttpheaders({'Content-Type':'application/json;charset=utf-8'});
返回this.http.get(this.csvPath,{responseType:'text'});
}
getData():CSVRecord[]{
this.userData=[];
this.getUserData().subscribe(数据=>{
data=“\uFEFF”+数据
控制台日志(数据);
const list=data.split('\n');
list.forEach(e=>{
常量属性=e.split(“;”);
this.temp={姓名:'',姓氏:'',邮政编码:'',城市:'',电话:'};
this.temp.name=属性[0];
this.temp.last_name=属性[1];
this.temp.postal_code=属性[2];
this.temp.city=物业[3];
this.temp.phone=属性[4];
this.userData.push(this.temp);
}); 
}); 
返回this.userData;
}
}
导出接口CSVRecord{
名称:字符串;
姓氏:字符串;
邮政编码:字符串;
城市:字符串;
电话:字符串;
}
还有。。。csv结果不返回国际字符。。这是我的另一个问题。。我是一个失败者哈哈 先谢谢你

----------编辑

我在这里读取值(角度材料表):

从'@angular/core'导入{Component,OnInit,ViewChild};
从'@angular/material/paginator'导入{MatPaginator};
从“@angular/material/table”导入{MatTableDataSource};
从“/../csv.service”导入{CSVService,CSVRecord};
@组成部分({
选择器:“应用程序数据表”,
templateUrl:'./data table.component.html',
样式URL:['./数据表.component.css']
})
导出类TablePaginationExample实现OnInit{
显示的列:字符串[]=['name'、'last_name'、'邮政编码'、'城市'、'电话'];
数据源=新MatTableDataSource([]);
@ViewChild(MatPaginator,{static:true})paginator:MatPaginator;
建造师(
专用csvService:csvService
) { }
恩戈尼尼特(){
this.dataSource.paginator=this.paginator;
}
刷新(){
this.csvService.doSomething().subscribe((数据:CSVRecord[])=>{
this.dataSource.data=数据;
});
}
myFunc(){
控制台日志(“učitaj”)
这个。刷新()
}
myFunc2(){
控制台日志(“spremi”)
}
}
----------编辑2 谢谢大家友好的回答:-)我现在更了解这一切了,而且它很有效

不需要doSomething()方法。您可以直接使用getData()

    import { Injectable } from '@angular/core';
    import { HttpClient, HttpHeaders } from '@angular/common/http';
    import { Observable, of, TimeoutError } from 'rxjs';
    import { getLocaleDateFormat } from '@angular/common';

    @Injectable({
      providedIn: 'root'
    })
    export class CSVService {

      csvPath = "podaci.csv";
      userList: any[] = [];

      constructor(private http: HttpClient) { }

      doSomething(): Observable<CSVRecord[]> {
        let csvList = this.getData();
        return of(csvList);
      }

      getUserData() {
        const headers = new HttpHeaders({'Content-Type':'application/json; charset=utf-8'});
        return this.http.get(this.csvPath, {responseType: 'text'});
      }

      getData(): Observable<CSVRecord[]> {
        const userData: CSVRecord[] = [];
        this.getUserData().map(data => {
          data = "\uFEFF"+data
          console.log(data);
          const list = data.split('\n');
            list.forEach( e => {
              const property = e.split(';');

             userData.push({
               name: property[0],
               last_name: property[1],
               postal_code: property[2]
               city: property[3],
               phone: property[4]
             });
            }); 
             return userData;
          }); 

      }

    }

    export interface CSVRecord {
      name: string;
      last_name: string;
      postal_code: string;
      city: string;
      phone: string;
    }
从'@angular/core'导入{Injectable};
从'@angular/common/http'导入{HttpClient,HttpHeaders};
从“rxjs”导入{observatable,of,TimeoutError};
从“@angular/common”导入{getLocaleDataFormat};
@注射的({
providedIn:'根'
})
导出类CSVService{
csvPath=“podaci.csv”;
用户列表:任意[]=[];
构造函数(私有http:HttpClient){}
doSomething():可观察{
设csvList=this.getData();
返回(csvList);
}
getUserData(){
constheaders=newhttpheaders({'Content-Type':'application/json;charset=utf-8'});
返回this.http.get(this.csvPath,{responseType:'text'});
}
getData():可观察{
const userData:CSVRecord[]=[];
this.getUserData().map(数据=>{
data=“\uFEFF”+数据
控制台日志(数据);
const list=data.split('\n');
list.forEach(e=>{
常量属性=e.split(“;”);
userData.push({
名称:属性[0],
姓氏:属性[1],
邮政编码:物业[2]
城市:地产[3],
电话:物业[4]
});
}); 
返回用户数据;
}); 
}
}
导出接口CSVRecord{
名称:字符串;
姓氏:字符串;
邮政编码:字符串;
城市:字符串;
电话:字符串;
}
不需要doSomething()方法。您可以直接使用getData()

    import { Injectable } from '@angular/core';
    import { HttpClient, HttpHeaders } from '@angular/common/http';
    import { Observable, of, TimeoutError } from 'rxjs';
    import { getLocaleDateFormat } from '@angular/common';

    @Injectable({
      providedIn: 'root'
    })
    export class CSVService {

      csvPath = "podaci.csv";
      userList: any[] = [];

      constructor(private http: HttpClient) { }

      doSomething(): Observable<CSVRecord[]> {
        let csvList = this.getData();
        return of(csvList);
      }

      getUserData() {
        const headers = new HttpHeaders({'Content-Type':'application/json; charset=utf-8'});
        return this.http.get(this.csvPath, {responseType: 'text'});
      }

      getData(): Observable<CSVRecord[]> {
        const userData: CSVRecord[] = [];
        this.getUserData().map(data => {
          data = "\uFEFF"+data
          console.log(data);
          const list = data.split('\n');
            list.forEach( e => {
              const property = e.split(';');

             userData.push({
               name: property[0],
               last_name: property[1],
               postal_code: property[2]
               city: property[3],
               phone: property[4]
             });
            }); 
             return userData;
          }); 

      }

    }

    export interface CSVRecord {
      name: string;
      last_name: string;
      postal_code: string;
      city: string;
      phone: string;
    }
从'@angular/core'导入{Injectable};
从'@angular/common/http'导入{HttpClient,HttpHeaders};
从“rxjs”导入{observatable,of,TimeoutError};
从“@angular/common”导入{getLocaleDataFormat};
@注射的({
providedIn:'根'
})
导出类CSVService{
csvPath=“podaci.csv”;
用户列表:任意[]=[];
构造函数(私有http:HttpClient){}
doSomething():可观察{
设csvList=this.getData();
返回(csvList);
}
getUserData(){
constheaders=newhttpheaders({'Content-Type':'application/json;charset=utf-8'});
返回this.http.get(this.csvPath,{responseType:'text'});
}
getData():可观察{
const userData:CSVRecord[]=[];
this.getUserData().map(数据=>{
data=“\uFEFF”+数据
控制台日志(数据);
const list=data.split('\n');
list.forEach(e=>{
常量属性=e.split(“;”);
userData.push({
名称:属性[0],
姓氏:属性[1],
doSomething(): Observable<CSVRecord[]> {
 let csvList = this.getData().pipe(
   switchMap((data) => {
      return of(data)
   })
 )
}
getData(): Observable<CSVRecord[]> {
this.userData = [];
return this.getUserData().pipe(map(
 (data) => {
    data = "\uFEFF"+data
    console.log(data);
    const list = data.split('\n');
    list.forEach( e => {
      const property = e.split(';');
      this.temp = {name:'', last_name:'', postal_code:'', city:'', phone:''};

      this.temp.name = property[0];
      this.temp.last_name = property[1];
      this.temp.postal_code = property[2];
      this.temp.city = property[3];
      this.temp.phone = property[4];

      this.userData.push(this.temp);
    }); 
    return userData;
  })); 
}