角度类型&#39; Observable <message []>&#39;不能分配给&#39; Observable <message []>&#39;

时间:2016-11-26 00:27:02

标签: angular ionic-framework

Noob到Angular2。我正在关注whatsapp克隆的教程并遇到了这个错误。

  

键入&#39; Observable&#39;不能分配给&#39; Observable&#39;。财产来源&#39;受到保护,但键入&#39; Observable&#39;不是来自&#39; Observable&#39;。

的类
import { Component, OnInit } from '@angular/core';
import { NavParams } from 'ionic-angular';
import { Chat, Message } from "api/models/chatsModel";
import { Messages } from "../../../api/collections/chatsCollection";
import { Observable } from "rxjs";

/*
  Generated class for the Messages page.

  See http://ionicframework.com/docs/v2/components/#navigation for more info on
  Ionic pages and navigation.
*/
@Component({
  selector: 'page-messages',
  templateUrl: 'messages.html'
})
export class MessagesPage implements OnInit {

    selectedChat: Chat;
    title: string;
    picture: string;
    messages: Observable<Message[]>;

  constructor(navParams: NavParams) {
    this.selectedChat = <Chat>navParams.get('chat');
    this.title = this.selectedChat.title;
    this.picture = this.selectedChat.picture;
  }

  ngOnInit() {
    let isEven = false;

    this.messages = Messages.find({chatId: this.selectedChat._id},
        { sort: {createdAt: 1} }
        ).map((messages: Message[]) => {
            messages.forEach((message: Message) => {
                message.ownership = isEven ? 'mine' : 'other';
                isEven = !isEven;
            });
            return messages;
        });
  }

}

我知道this.messages不接受跟随它的代码,因为它与预期的类型不同,但由于我是如此新,我不太确定如何更改它以使其成为可能接受。任何帮助是极大的赞赏。谢谢!

1 个答案:

答案 0 :(得分:0)

确保Observable和Message都是从同一个地方导入的,并且都是类或接口而不是混合