Angular 离子和角函数不返回值
我无法让Angular 离子和角函数不返回值,angular,ionic-framework,Angular,Ionic Framework,我无法让addReservation函数返回任何值 在reservations.page.ts中,当我登录到控制台时,它返回一个空数组 userReservations: Reservation[]; private resSubscription: Subscription; constructor(private resService: ReservationsService) { } ngOnInit() { this.resSubscription =
addReservation
函数返回任何值
在reservations.page.ts
中,当我登录到控制台时,它返回一个空数组
userReservations: Reservation[];
private resSubscription: Subscription;
constructor(private resService: ReservationsService) { }
ngOnInit() {
this.resSubscription =
this.resService.reservations.subscribe(reservations => {
console.log('reservations...', reservations);
this.userReservations = reservations;
});
}
下面是reservations.service.ts
我无法让return语句中的reservations记录任何值,因此我猜测可能存在问题
export class ReservationsService {
private _reservations = new BehaviorSubject<Reservation[]>([]);
constructor(private authentication: AuthenticationService) { }
// return the parking space reservations
get reservations() {
return this._reservations.asObservable();
}
// create a reservation
addReservation(
parkingSpaceId: string,
parkingSpaceTitle: string,
url: string,
firstName: string,
lastName: string,
reservedDayCount: number,
reservedFrom: Date,
reservedTo: Date
) {
const newReservation = new Reservation(
Math.random().toString(),
parkingSpaceId,
this.authentication.userId,
parkingSpaceTitle,
url,
firstName,
lastName,
reservedDayCount,
reservedFrom,
reservedTo
);
return this.reservations.pipe(
take(1),
tap(reservations => {
console.log('return reservations...', reservations);
this._reservations.next(reservations.concat(newReservation));
})
);
}
}
导出类保留服务{
private _reservations=新行为主体([]);
构造函数(私有身份验证:AuthenticationService){}
//归还停车位预订
获得预定{
返回此项。_reservations.asObservable();
}
//创建预订
添加预订(
parkingSpaceId:string,
parkingSpaceTitle:字符串,
url:string,
名字:string,
姓氏:string,
ReservedCount:编号,
保留日期:年月日,
保留日期:年月日
) {
const newReservation=新预订(
Math.random().toString(),
parkingSpaceId,
this.authentication.userId,
parkingSpaceTitle,
网址,
名字,
姓,
储备账户,
保留自,
保留给
);
把这个还给我(
以(1)为例,
点击(预订=>{
log('returnreservations…',reservations);
这个._reservations.next(reservations.concat(newReservation));
})
);
}
}
问题出在您的addReservation
功能中
按照您的方式,只有在订阅了addReservation
时,点击才会执行。。。这是错误的,因为这将导致无限循环
下面的链接显示了如何专门使用Rxjs来管理Angular service中的状态,而不必存储数据的任何本地副本:
以下是将模式应用于ReservationsService的方式:
export class ReservationsService {
get reservations(): Reservation[] {
return this._reservations.getValue();
}
private set reservations(val: Reservation[]) {
this._reservations.next(val);
}
private _reservations = new BehaviorSubject<Reservation[]>([]);
constructor(private authentication: AuthenticationService) { }
// return the parking space reservations
get reservations$() {
return this._reservations.asObservable();
}
// create a reservation
addReservation(
parkingSpaceId: string,
parkingSpaceTitle: string,
url: string,
firstName: string,
lastName: string,
reservedDayCount: number,
reservedFrom: Date,
reservedTo: Date
) {
const newReservation = new Reservation(
Math.random().toString(),
parkingSpaceId,
this.authentication.userId,
parkingSpaceTitle,
url,
firstName,
lastName,
reservedDayCount,
reservedFrom,
reservedTo
);
this.reservations = [
...this.reservations,
newReservation
];
}
}
导出类保留服务{
获取预订():预订[]{
返回此项。_reservations.getValue();
}
专用集预订(val:Reservation[]){
这个。_预订。下一个(val);
}
private _reservations=新行为主体([]);
构造函数(私有身份验证:AuthenticationService){}
//归还停车位预订
获得预订($){
返回此项。_reservations.asObservable();
}
//创建预订
添加预订(
parkingSpaceId:string,
parkingSpaceTitle:字符串,
url:string,
名字:string,
姓氏:string,
ReservedCount:编号,
保留日期:年月日,
保留日期:年月日
) {
const newReservation=新预订(
Math.random().toString(),
parkingSpaceId,
this.authentication.userId,
parkingSpaceTitle,
网址,
名字,
姓,
储备账户,
保留自,
保留给
);
这是预订=[
…这是预订,
新保留地
];
}
}
我必须在reservations.page.ts中执行这个.reservice.reservations
对吗?使用this.reservations I get属性不存在。能否显示在何处使用“addReservation”?类似于这样:如果我使用this.\u reservations.next(this.reservations.concat(newReservation))代码>我获取属性“concat”在类型“Observable”上不存在,对吗。。。您需要存储对阵列的本地引用,请举例说明?