简体   繁体   English

无法获取可观察值

[英]Fail to retrieve Observable value

I have aa function that returns an Observable<Person[]> , Person is my model: 我有一个返回Observable<Person[]>函数,Person是我的模型:

export interface Person {
  id: string;
  age: number;
}

Now in my component.ts im calling this function and I want to retrieve that array so I can display it in the html. 现在在我的component.ts中,我正在调用此函数,我想检索该数组,以便可以在html中显示它。

the array that comes back example: 返回的数组示例:

[{"id":"13434 1","age":21},{"id":"34334 1","age":27}]

I have 2 buttons that call the same function. 我有2个调用相同功能的按钮。

I have three methods in my component.ts that triggered when that buttons where clicked, and this is where I want to set some variable to hold the returned value of that Observable. 我的component.ts中有三种方法,它们是在单击该按钮时触发的,这是我想要设置一些变量以保存该Observable返回值的地方。 I'v tried to do something but it didnt work... 我试图做点什么,但没有成功...

this are the functions: 这些是功能:

@Injectable()
export class MyCmp implements OnInit {

  listOneData: Observable<Person[]>;
  listTwoData: Observable<Animal[]>;

  showListOne = false;
  showListTwo = false;

  constructor(private _myService: MyService) {
  };

  public showListOneData(): void {
    this.showListOne = true;
    this.showListTwo = false;
    this._myService.getListOneData().subscribe(res => {
      this.listOneData = res;
    })
  }

  public showListTwo(): void {
    this.showListTwo = true;
    this.showListOne = false;
    this._myService.getListTwoData().subscribe(res => {
      this.listTwoData = res;
    })
  }
}

this.listTwoData = res; this line does not compile, its because im assigning Person[] to listOneData: Observable<Person[]>; 这行不会编译,因为我将Person[]分配给listOneData: Observable<Person[]>; , but even if I take of the Observable<> it dosent work. ,但是即使我采用了Observable <>,它也可以正常工作。

Can someone please explain to me what woill be an efficiant way to do it? 有人可以告诉我什么方法可以有效地做到这一点吗? I dont want to do it async cause I want to send an array to the html, and base on the code where should I do unsubscribe? 我不想异步执行此操作,因为我想将数组发送到html,并且基于代码我应该在哪里退订?

thanks allot! 谢谢分配!

Sounds like you want two different things; 听起来您想要两种不同的东西; data from the observable and a subscription object so you can unsubscribe: 来自可观察对象和订阅对象的数据,因此您可以退订:

@Injectable()
export class MyCmp implements OnInit {

  listOneData: Person[];
  listTwoData: Animal[];

  listOneSubscription: Subscription;    
  listTwoSubscription: Subscription;

  showListOne = false;
  showListTwo = false;

  constructor(private _myService: MyService) {
  };

  public showListOneData(): void {
    this.listOneSubscription = this._myService.getListOneData().subscribe(res => {
      this.showListOne = true;
      this.showListTwo = false;
      this.listOneData = res;
    })
  }

  public showListTwo(): void {
    this.listTwoSubscription = this._myService.getListTwoData().subscribe(res => {
      this.showListTwo = true;
      this.showListOne = false;
      this.listTwoData = res;
    })
  }
}

Then, wherever you're wanting to unsubscribe: 然后,无论您要退订什么地方:

this.listOneSubscription && this.listOneSubscription.unsubscribe();
this.listTwoSubscription && this.listTwoSubscription.unsubscribe();

You also mentioned you don't want to do it async; 您还提到了您不想异步执行它。 that doesn't make sense in JavaScript since this is asynchronous; 因为这异步的,所以在JavaScript中没有意义。 ask yourself, what do you want to show to the user while the content is loading? 问问自己,您想在内容加载时向用户显示什么? You probably want to show some sort of progress indicator, so that should be built into your view. 您可能想要显示某种进度指示器,因此应该将其内置到视图中。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM