1

我的聊天服务出现此错误我无法解决
这是错误

这是我的聊天服务的实际代码

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;
    }

}  
4

1 回答 1

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

    @NgModule({
        providers: [
            ChatService
        ]
    })
    
于 2018-01-06T16:18:17.900 回答