angular - typescript - Angular : static injector error

标签 angular typescript sockets dependency-injection

您好,我正在尝试在我的 Angular 项目中使用 socket.io。 我将展示三个文件,它们是组件文件、一个服务文件和一个模块文件。每当我在我的组件文件中使用服务时,我都会收到静态注入(inject)器错误。 这是:

Error: StaticInjectorError(AppModule)[AppComponent -> WrappedSocket]: StaticInjectorError(Platform: core)[AppComponent -> WrappedSocket]:
NullInjectorError: No provider for WrappedSocket!

组件文件如下:

import { Component } from '@angular/core';   
import {  cheema2 } from './practice.service';
import { Injectable } from '@angular/core';
import { Socket } from 'ng-socket-io';

@Component
({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})

 @Injectable()    
 export class AppComponent  
 {   
     constructor(private socket: Socket) { }

     sendMessage(msg: string){
     this.socket.emit("message", msg);
   }

   getMessage() 
   {
     console.log( this.socket.fromEvent("message"));
   }

}

这是模块文件

import { BrowserModule } from '@angular/platform-browser';   
import { NgModule } from '@angular/core';
import { SocketIoModule, SocketIoConfig } from 'ng-socket-io';

const config: SocketIoConfig = { url: 'http://localhost:4200', options: {}    
};       
import { AppComponent } from './app.component';

@NgModule
({  
declarations:
 [
   AppComponent
 ],
 imports: [
   BrowserModule
  ],
 providers: [],
 bootstrap: [AppComponent]
})

export class AppModule { }

这是服务文件

import { Injectable } from '@angular/core';  
import { Socket } from 'ng-socket-io';

@Injectable()
export class cheema2  
{
    constructor(private socket: Socket) { console.log("adil"); }

    sendMessage(msg: string){
       console.log("sharif");
       this.socket.emit("message", msg);
    }

    getMessage() {
      console.log( this.socket.fromEvent("message"));              
    }
}

任何可以解决此错误的人。

最佳答案

您的 AppModule 中缺少导入:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { SocketIoModule, SocketIoConfig } from 'ng-socket-io';
const config: SocketIoConfig = { 
  url: 'http://localhost:4200', options: {}
};

import { AppComponent } from './app.component';

@NgModule ({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    SocketIoModule.forRoot(config) <<< ADD THIS
  ],
  providers: [],
  bootstrap: [AppComponent] })
export class AppModule { }

关于angular - typescript - Angular : static injector error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50233403/

相关文章:

javascript - Firebase云函数: Error: EISDIR: illegal operation on a directory

typescript - 如何检查 Typescript Set 数据结构中的列表相等性?

c++ - 线程等待父级

python - 使用 SOCK_STREAM 代理处理 https 请求

angular - 根据先前响应的结果顺序级联 Observables 并重新映射到新数据对象

Angular 8 : Make FormBuilder Accept Numbers Only with Range for Latitude and Longitude

javascript - 在 Angular 4/5/6/7 中对不同组件使用不同的样式表

java - 如何使用套接字与socketio交互

Angular 2 : Error: unexpected value 'AppComponent' declared by the module 'AppModule'

angular - 使用 Angular CLI 时转译的 js 文件在哪里