【问题标题】:NodeInvocationException: No provider for SignalServiceNodeInvocationException:没有 SignalService 的提供者
【发布时间】:2019-01-19 01:00:26
【问题描述】:

我正在使用服务结构,并且能够编译我的应用程序,但是当我连接到它时,我收到以下错误:

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

从那里,我将服务添加到提供程序,这就是它所抱怨的并且一切都编译了 - 但它没有运行。

【问题讨论】:

    标签: typescript signalr angular5 azure-service-fabric


    【解决方案1】:

    显然,它进入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
        ]
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-26
      • 2017-08-13
      • 2012-07-07
      • 2018-06-29
      • 2018-08-11
      • 2017-06-04
      • 2018-04-24
      • 2017-03-01
      相关资源
      最近更新 更多