简体   繁体   English

如何编写将返回的函数:Observable<ModelTemp>

[英]How to write a function which will be return : Observable <ModelTemp>

How to write a function which will be return : Observable .如何编写将返回的函数:Observable。

my service:我的服务:

 modelTemp= [{ 
    name: 'abcdefghijk'
 }];   
 get(): Observable<ModelTemp> {
    return this.modelTemp;
 }

In service the method the variable is displayed red:在服务方法中,变量显示为红色:

Returned expression type {name: string}[] is not assignable to type Observable less... (Ctrl+F1) Checks TypeScript called function parameters , return values , assigned expressions to be of correct type返回的表达式类型 {name: string}[] 不可分配给类型 Observable less... (Ctrl+F1) 检查 TypeScript 调用的函数参数、返回值、分配的表达式是否为正确类型

How to fix this error?如何修复此错误?

my component:我的组件:

 model: ModelTemp; 

    constructor(private getService: GetService) {}
    ngOnInit() {
      this.generate();
    }

    generate() {
      this.getService.get()
        .subscribe((modelData) => { 
          this.model = modelData;
        });
    }

my interface:我的界面:

   export interface ModelTemp {
      name: string; 
   }

I do not know where I'm making a mistake.我不知道我在哪里犯了错误。 The model component should be declared differently (in this way): modelPage: ModelPage;模型组件应该以不同的方式声明(以这种方式): modelPage: ModelPage; or so: modelPage: ModelPage[];左右:模型页:模型页[];

In my the browser returns me an error:在我的浏览器中,我返回一个错误:

this.getService.get(...).subscribe is not a function at TempComponent.push../src/app/temp/temp.component.ts.TempComponent.generate. this.getService.get(...).subscribe 不是 TempComponent.push../src/app/temp/temp.component.ts.TempComponent.generate 的函数。

I want to use the subscribe method.我想使用订阅方法。 Which will return the model (interface ModelTemp)这将返回模型(接口 ModelTemp)

How to correct the code?如何更正代码?

How to write a function which will be return : Observable .如何编写将返回的函数:Observable。

use rxjs operators like of to return an Observable使用像of 的rxjs 操作符返回一个 Observable

import {of} from 'rxjs';

 get(): Observable<ModelTemp> {
  return of(this.modelTemp);
} 

You can try something like this:你可以尝试这样的事情:

modelTemp: BehaviorSubject<ModelTemp[]> = new BehaviorSubject([{ 
    name: 'abcdefghijk'
}]);

get(): BehaviorSubject <ModelTemp[]> {
    return this.modelTemp;
}

This returns a Subject that you can subscribe to.这将返回一个您可以订阅的主题。

It depends on what sort of control you need over the resulting observable.这取决于您需要对生成的 observable 进行什么样的控制。

  • If you need only to satisfy a requirement that the already-known data be in an observable, then Fateh Mohamed has the right idea in returning a of(this.modelTemp) .如果您只需要满足已知数据在 observable 中的要求,那么Fateh Mohamed返回 a of(this.modelTemp)想法是正确的。

  • But if you need to emit future values as the ModelTemp array changes, then German Burgardt is on a better track by providing you a Subject on which you can call behaviorSubject.next(newModelTemp) which will emit to anyone who subscribed to your Behavior Subject .但是,如果您需要在 ModelTemp 数组更改时发出未来值,那么German Burgardt可以通过为您提供一个主题来更好地跟踪,您可以在该主题上调用behaviorSubject.next(newModelTemp) ,该主题将发送给订阅您的Behavior Subject 的任何人。 You may also wish to prevent other callers from emitting from this behavior subject by returning only the result of behaviorSubject.asObservable() and keeping the behavior subject itself private.您可能还希望通过仅返回behaviorSubject.asObservable()的结果并保持行为主体本身的私有来防止其他调用者从此行为主体发出信号。

  • Lastly, if the ModelTemp array contains a set of values that should be emitted in succession by the observable, use from(this.modelTemp) .最后,如果 ModelTemp 数组包含一组应由 observable 连续发出的值,请使用from(this.modelTemp)

the method: get(): Observable<ModelTemp> { return of(this.modelTemp); }方法: get(): Observable<ModelTemp> { return of(this.modelTemp); } get(): Observable<ModelTemp> { return of(this.modelTemp); } will reset the error: Type 'Observable<{ name: string; }[]>' is not assignable to type 'Observable<ModelTemp>'. Property 'name' is missing in type '{ name: string; }[]' but required in type 'ModelTemp'. get(): Observable<ModelTemp> { return of(this.modelTemp); }将重置错误: Type 'Observable<{ name: string; }[]>' is not assignable to type 'Observable<ModelTemp>'. Property 'name' is missing in type '{ name: string; }[]' but required in type 'ModelTemp'. Type 'Observable<{ name: string; }[]>' is not assignable to type 'Observable<ModelTemp>'. Property 'name' is missing in type '{ name: string; }[]' but required in type 'ModelTemp'.

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

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