我想在服务器1秒后自动获取聊天但是当我使用可观察计时器时出错,请建议我。 我在控制台中遇到的错误在引号中: -
" Response {_body: ProgressEvent, status: 0, ok: false, statusText: "", headers: Headers…} "
这是我在angular 2.0中使用的代码,在函数ngOnInit中我使用的计时器在控制台中几秒后就给我发错了。
import { Component, OnInit } from '@angular/core';
import { Response } from "@angular/http";
import { NgForm } from "@angular/forms";
import {Observable} from 'rxjs/Rx';
import { Chat } from "../chat.interface";
import { ChatService } from '../chat.service';
@Component({
selector: 'app-chat-box',
templateUrl: './chat-box.component.html',
styleUrls: ['./chat-box.component.css'],
providers: [ChatService]
})
export class ChatBoxComponent implements OnInit {
thumb="http://www.lovemarks.com/wp-content/uploads/profile-avatars/default-avatar.png";
data = [];
messages = [];
error ='';
constructor(private chatService: ChatService) {
}
ngOnInit() {
let timer = Observable.timer(10, 500);
timer.subscribe(() => this.newMessage());
}
bubble(){
this.chatService.getMessages()
.subscribe(
(data) => this.data = this.data,
(error: Response) => console.log(error)
);
}
newMessage(){
this.chatService.checkMessages()
.subscribe(
(data) => this.messages = this.messages.concat(data.messages),
(error: Response) => console.log(error)
);
}
onSubmit(form: NgForm) {
this.chatService.createMessage(form.value.message)
.subscribe(
() => this.newMessage()
);
form.reset();
}
}