errors.ts:42错误错误:未捕获(承诺):

时间:2018-01-05 23:52:33

标签: angular angular-providers

我的聊天服务中出现此错误,我无法解决问题    Here's the error

And i have the following code

这是实际代码

import{Injectable} from '@angular/core';
import {Subject} from 'rxjs/Subject';
import {Observable} from 'rxjs/Observable';
 import * as io from 'socket.io-client';
export class chatService{
    private url = 'http://localhost:8000'
    private socket:any;

    sendMessage(message:string){
        this.socket.emit('add-message', message);
    }
    getMessages(){
        let observable = new Observable(( observer:any)=>{
            this.socket = io(this.url);
            this.socket.on('message',(data:any)=>{
                observer.next(data);
            });
            return () => {
                this.socket.disconnect();
            }
        })
        return observable;
    }

}  

1 个答案:

答案 0 :(得分:0)

  1. 首先,根据Angular Style Guide,您应该使用大写字母表示您的服务(又名。chatService => ChatService
  2. 接下来,您是否在应用程序的模块文件中包含ChatService作为提供程序?

    @NgModule({
        providers: [
            ChatService
        ]
    })