我正在使用服务结构,并且能够编译我的应用程序,但是当我连接到它时,出现以下错误:
An unhandled exception occurred while processing the request.
NodeInvocationException: No provider for SignalService!
Error: No provider for SignalService!
signal.service.ts
import { Injectable, OnInit } from '@angular/core';
import { HubConnection } from '@aspnet/signalr';
import * as signalr from '@aspnet/signalr';
@Injectable()
export class SignalService implements OnInit {
private _aphub: HubConnection;
public aphub: HubConnection = this._aphub;
ngOnInit(): void {
this._aphub.on("connect", () => {
console.log("Connect invoked");
});
let p = this._aphub.start();
Promise.all([p])
.then(() => {
this.aphub = this._aphub;
})
.catch(reason => {
console.log(reason);
});
}
constructor() {
let options = {
transport: signalr.HttpTransportType.WebSockets
| signalr.HttpTransportType.LongPolling,
AccessTokenFactory: async () => {
// Get and return the access token.
// This function can return a JavaScript Promise if asynchronous
// logic is required to retrieve the access token.
}
};
let protocol = new signalr.JsonHubProtocol();
this._aphub = new signalr.HubConnectionBuilder()
.configureLogging(signalr.LogLevel.Trace).withUrl('/aphub', options)
.withHubProtocol(protocol).build();
}
}
应该非常简单,建立集线器连接,设置至少一个on
事件并启动连接。
app.browser.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppModuleShared } from './app.shared.module';
import { AppComponent } from './components/app/app.component';
import { SignalService } from "./signal.service";
@NgModule({
bootstrap: [AppComponent],
imports: [
BrowserModule,
AppModuleShared
],
providers: [
{ provide: 'BASE_URL', useFactory: getBaseUrl },
SignalService
]
})
export class AppModule {
}
export function getBaseUrl() {
return document.getElementsByTagName('base')[0].href;
}
从那里,我将服务添加到提供程序中,这正是它所抱怨的,并且所有内容都可以编译-但无法运行。
答案 0 :(得分:0)
显然,它进入了app.shared.module.ts
@NgModule({
declarations: [
AppComponent,
NavMenuComponent,
CounterComponent,
FetchDataComponent,
HomeComponent
],
imports: [
CommonModule,
HttpModule,
FormsModule,
RouterModule.forRoot([
{ path: '', redirectTo: 'home', pathMatch: 'full' },
{ path: 'home', component: HomeComponent },
{ path: 'counter', component: CounterComponent },
{ path: 'fetch-data', component: FetchDataComponent },
{ path: '**', redirectTo: 'home' }
])
], providers: [
SignalService
]
})