Angular 如何解决类型';上不存在属性subscribe;编号';应用程序出错?

Angular 如何解决类型';上不存在属性subscribe;编号';应用程序出错?,angular,typescript,ionic-framework,Angular,Typescript,Ionic Framework,下面的代码在Ionic 5/Angular教程中用于更新Place对象。我正在尝试将相同的代码应用到我的项目中,在该项目中我更新了一个对话对象: 地点服务: 如您所见,调用时可以订阅上面的updatePlace()方法 我正在尝试创建一个也可以订阅的addMessageToConversation()方法,与上面的方法完全相同 以下是我目前的代码: 对话服务: 上面的代码确实更新了对话,但我无法订阅该方法。有人能告诉我需要做些什么改变才能做到这一点吗 如果我返回addMessageToConve

下面的代码在Ionic 5/Angular教程中用于更新
Place
对象。我正在尝试将相同的代码应用到我的项目中,在该项目中我更新了一个
对话
对象:

地点服务:

如您所见,调用时可以订阅上面的
updatePlace()
方法

我正在尝试创建一个也可以订阅的
addMessageToConversation()
方法,与上面的方法完全相同

以下是我目前的代码:

对话服务:

上面的代码确实更新了
对话
,但我无法订阅该方法。有人能告诉我需要做些什么改变才能做到这一点吗

如果我
返回
addMessageToConversation()
中的内容,并尝试
订阅它,我会收到以下错误消息:

类型“number”上不存在属性
subscribe

此外,如果我试图从
此.u conversations.getValue().find(conversation=>…
)中删除
,则会收到以下错误消息:

类型上不存在属性“getValue” “可观察的”


您可以从
BehaviorSubject
中直接获取conversations数组,因为它总是将最后一个值重新发送给新订阅者

从'rxjs/operators'导入{switchMap};
从'rxjs'导入{empty};
//Obs:注意,我们返回了一个可观测的空值(末尾的空(),
//避免为修改后的对话返回过期值。
addMessageToConversation(
会话ID:string,
消息:string
):可见{
把这个还给我(
以(1)为例,
map((conversations)=>conversations.find(c=>c.id==conversationId)),
轻触((c:对话)=>{
//返回案例c或消息是错误的
如果(!c?.messages){return;}
c、 messages.push(
新消息(
Math.random().toString(),
消息
this.authService.userId,
新日期(Date.now())
));
//重新发出对话数组(使用新引用)
this.\u conversations.next(JSON.parse(JSON.stringify(this.\u conversations));
}),
//此步骤是可选的,只是为了避免返回过时的对话
switchMap((_oldConversation:Conversation)=>empty()
);
}
您可以这样使用它:

addMessageToConversation('conversation1','嘿,看!一条新消息')
.subscribe();
private _places = new BehaviorSubject<Place[]>([
    new Place(
      'p1',
      'Manhattan Mansion',
      'In the heart of New York City.',
      'https://lonelyplanetimages.imgix.net/mastheads/GettyImages-538096543_medium.jpg?sharp=10&vib=20&w=1200',
      149.99,
      new Date('2019-01-01'),
      new Date('2019-12-31'),
      'abc'
    )
  ]);

  get places() {
    return this._places.asObservable();
  }

updatePlace(placeId: string, title: string, description: string) {
    return this.places.pipe(
      take(1),
      delay(1000),
      tap(places => {
        const updatedPlaceIndex = places.findIndex(pl => pl.id === placeId);
        const updatedPlaces = [...places];
        const oldPlace = updatedPlaces[updatedPlaceIndex];
        updatedPlaces[updatedPlaceIndex] = new Place(
          oldPlace.id,
          title,
          description,
          oldPlace.imageUrl,
          oldPlace.price,
          oldPlace.availableFrom,
          oldPlace.availableTo,
          oldPlace.userId
        );
        this._places.next(updatedPlaces);
      })
    );
  }
export class Place {
  constructor(
    public id: string,
    public title: string,
    public description: string,
    public imageUrl: string,
    public price: number,
    public availableFrom: Date,
    public availableTo: Date,
    public userId: string
  ) {}
}
private _conversations = new BehaviorSubject<Conversation[]>([
    new Conversation(
      'conversation1',
      'user3',
      'user1',
      [
        new Message('message1', 'Test message', 'user3', new Date(2018, 0O5, 0O5, 17, 23, 42, 11)),
        new Message('message2', 'Another message', 'user1', new Date(2018, 0O6, 0O5, 17, 23, 42, 11))
      ])
  ]);

  get conversations() {
    return this._conversations.asObservable();
  }

addMessageToConversation(conversationId: string, message: string) {
    this._conversations.getValue().find(conversation => conversation.id === conversationId)
      .messages.push(
        new Message(
          Math.random().toString(),
          message,
          this.authService.userId,
          new Date(Date.now())
        ));
  }
export class Conversation {
    constructor(
        public id: string,
        public userId: string,
        public mechanicId: string,
        public messages: Message[]
    ) { }
}

export class Message {
    constructor(
        public id: string,
        public text: string,
        public userId: string,
        public timestamp: Date
    ) { }
}