[英]Typescript return type for Observables in getter and setter
I'm building an app using Angular 2 with Typescript.我正在使用 Angular 2 和 Typescript 构建一个应用程序。 What return type my setter and getter should have?
我的 setter 和 getter 应该有什么返回类型?
import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Events, SqlStorage, Storage } from 'ionic-angular';
import { Subject } from 'rxjs/Subject';
export interface User {
name?: string,
email?: string,
gender?: string,
picture?: Object
}
export interface DataStore {
user: User
}
@Injectable()
export class UserService {
private storage: Storage;
private _user$: Subject<User>;
private dataStore: DataStore;
constructor(
private http: Http
) {
this.storage = new Storage(SqlStorage);
this._user$ = new Subject();
this.dataStore = { user: {} };
}
set user$(user: User) {
this.storage.set('user', JSON.stringify(user));
this.dataStore.user = user;
this._user$.next(this.dataStore.user);
}
get user$() {
const subject = this._user$;
const source = subject.asObservable();
return source;
}
}
I have tried this:我试过这个:
set user$(user: User): void {}
get user$(): Observable<User> {}
But throw these errors:但是抛出这些错误:
Error TS2380: 'get' and 'set' accessor must have the same type.
错误 TS2380:“get”和“set”访问器必须具有相同的类型。 Error
错误
TS1095: A 'set' accessor cannot have a return type annotation.
TS1095:“set”访问器不能有返回类型注释。 Error
错误
TS2380: 'get' and 'set' accessor must have the same type.
TS2380:“get”和“set”访问器必须具有相同的类型。 Error
错误
TS2322: Type 'User' is not assignable to type 'Observable'.
TS2322:“用户”类型不可分配给“可观察”类型。
Error TS2322: Type 'User' is not assignable to type
错误 TS2322:类型“用户”不可分配给类型
'Observable'.
'可观察'。 Property '_isScalar' is missing in type 'User'.
“用户”类型中缺少属性“_isScalar”。
I think you're almost there but I question using the setter, at least in the code you have provided.我想你快到了,但我质疑使用 setter,至少在你提供的代码中。 When you look at the code inside your setter you're actually not setting anything to _user$.
当您查看 setter 中的代码时,您实际上并未对 _user$ 设置任何内容。
I think this is what is tripping you up, I don't think you should be using a setter here.我认为这是绊倒你的原因,我认为你不应该在这里使用二传手。 Based on what the code is doing inside your setter, this should just be a public function.
根据代码在您的 setter 中执行的操作,这应该只是一个公共函数。 This is what I think it should look like
这就是我认为它应该是什么样子
constructor(private http: Http) {
this.storage = new Storage(SqlStorage);
this._user$ = new Subject();
this.dataStore = { user: {} };
}
setUser(user: User) {
this.storage.set('user', JSON.stringify(user));
this.dataStore.user = user;
this._user$.next(this.dataStore.user);
}
get user$() {
return this._user$.asObservable();
}
Then your component that is using the Service, would call UserService.setUser(userObj).然后您使用该服务的组件将调用 UserService.setUser(userObj)。
When you follow errors messages, you will have only two pairs of setter and getter:当您遵循错误消息时,您将只有两对 setter 和 getter:
get var(): User {}
set var(user: User) {}
And并且
get var(): Observable<User> {}
set var(user$: Observable<User>) {}
Actually that are the only allowed combinations.实际上,这是唯一允许的组合。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.