简体   繁体   English

将数组转换为可观察的类型<T[]>

[英]Convert array to the type of Observable<T[]>

I want to convert an array to type Observable<T[]> and have used the rxjs method "from". 我想将数组转换为Observable<T[]> ,并使用了rxjs方法“ from”。 But it returns Observable<T> , Is there a way to convert an array to Observable<T[]> ? 但是它返回Observable<T> ,有没有办法将数组转换为Observable<T[]>

Player: Observable<T[]>;

convert(){
let tempArr:T[] = Object.values(data) as T[];
let Obsobj = from(tempArr);
this.Player = Obsobj;
}

This is the error message I got, Observable<T> is not assignable to type Observable<T[]> 这是我收到的错误消息, Observable<T>不能分配给Observable<T[]>

EDIT 编辑

Even though using following code snippet return type isn't as expected. 即使使用以下代码段,返回类型也不符合预期。

let $sub = new Subject<Array<T>>();
$sub.next(tempArr);
this.Player$ = $sub.asObservable();

Since I want the return observable type to be as follows. 由于我希望返回的可观察类型如下。

Observable {_isScalar: false, source: Observable, operator: MapOperator}

but returned the following. 但返回了以下内容。

Observable {_isScalar: false, source: Subject}

EDIT #2 编辑#2

complete code snippet. 完整的代码段。

Player: Observable<B[]>;

convert(data: Dictionary<B>){
   let tempArr:B[] = Object.values(data) as B[];
   let $sub = new Subject<Array<B>>();
   $sub.next(tempArr);
   this.Player$ = $sub.asObservable();
}

What is the issue here? 这是什么问题? Any help? 有什么帮助吗?

Observable.from Documentation: For arrays and iterables, all contained values will be emitted as a sequence! 从文档中观察到:对于数组和可迭代对象,所有包含的值将作为序列发出! So your items will always be emitted as T and not T[] 因此,您的项目将始终以T而不是T []的形式发出

For Observable<T[]> do either of the following: 对于Observable<T[]>请执行以下任一操作:

$sub = new Subject<Array<T>>();
this.Player = $sub.asObservable();

OR 要么

this.Player = of(array);

OR 要么

Observable.create((observer) => {observer.next(array)})

Use of operator instead of from . 使用of运营商,而不是from

import { of } from 'rxjs';
...

player: Observable<T[]>;

convert(){
  let tempArr:T[] = Object.values(data) as T[];
  let Obsobj = of(tempArr);
  this.Player = Obsobj;
}

This isn't how generics work. 这不是泛型的工作方式。

You will have to refactor your code in order to make it work. 您必须重构代码才能使其正常工作。

Ideally your convert method should expect a generic type T 理想情况下,您的convert方法应该期望泛型T

Something like this: 像这样:

convert<T>(array: Array<T>): Observable<Array<T>> {
  return of(array);
}

And when you call convert , you call it like this: 当您调用convert ,您将这样称呼它:

this.convert<User>([{ firstName: `John`, lastName: `Doe` }])
  .subscribe((userArray: Array<User>) => console.log(userArray));

When you do this, you are telling your convert function that you want the Type T to be a User type. 当您这样做时,您是在告诉您的convert函数您希望Type TUser类型。

Here's a Complete working Example: 这是一个完整的工作示例:

import { Component } from '@angular/core';
import { Observable, of } from 'rxjs';

interface User {
  firstName: string;
  lastName: string;
}

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  player: Observable<Array<User>>;

  ngOnInit() {
    this.convert<User>([{ firstName: `John`, lastName: `Doe` }])
      .subscribe((userArray: Array<User>) => console.log(userArray));
  }

  convert<T>(array: Array<T>): Observable<Array<T>> {
    return of(array);
  }
}

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

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