我使用Angular 2,TypeScript和SignalR設置.NET Core項目。我創建並使用了DefinitelyTyped的jQuery和SignalR類型。TypeScript找不到全局引用的SignalR接口擴展
幾篇文章(如one)建議我通過接口聲明中樞服務器和客戶端上可用的接口,爲我的SignalR中心添加一些強類型。
經過很多調整,我終於得到了IDE的快樂:它檢測接口和擴展沒有大驚小怪。但是,每當我開始構建時,TypeScript都會抱怨它找不到名稱'IRoomHub'(我的集線器接口名稱)。
這裏是我的接口的聲明,它被保存在一個文件hubs.d.ts在我的分型(全球安裝)signalr文件夾:
interface SignalR {
roomHub: IRoomHub;
}
interface IRoomHub {
client: IRoomHubClient;
server: IRoomHubServer;
}
interface IRoomHubClient {
initiateTimer: (startMinutes: number) => void;
}
interface IRoomHubServer {
initiateTimer(roomId: number, startMinutes: number): Promise<void>;
}
我的用法是在我的腳本文件夾:
import { Component, OnInit } from '@angular/core';
import { RoomService } from './room.service';
import { Room } from './room';
@Component({
selector: 'rooms',
template: `...`,
providers: [RoomService]
})
export class AdministrationPage implements OnInit {
roomHub: IRoomHub;
constructor(private roomService: RoomService) { }
rooms: Room[];
initialize(room: Room) {
this.roomHub.server.initiateTimer(room.id, room.startMinutes);
}
getRooms(): void {
this.roomService.getRooms().then(rooms => this.rooms = rooms);
}
ngOnInit(): void {
this.getRooms();
this.roomHub = $.connection.roomHub;
}
}
我tsConfig:
{
"compilerOptions": {
"noImplicitAny": false,
"noEmitOnError": true,
"removeComments": false,
"sourceMap": true,
"target": "es5",
"outDir": "../wwwroot/scripts/app",
"emitDecoratorMetadata": true,
"experimentalDecorators": true
},
"exclude": [
"node_modules",
"wwwroot"
]
}
和我分型index.d.ts文件:
/// <reference path="globals/core-js/index.d.ts" />
/// <reference path="globals/jasmine/index.d.ts" />
/// <reference path="globals/jquery/index.d.ts" />
/// <reference path="globals/node/index.d.ts" />
/// <reference path="globals/signalr/index.d.ts" />
/// <reference path="globals/signalr/hubs.d.ts" />
我試圖從接口更改爲導出接口,在申報命名空間和申報模塊周圍,但是編譯器總是有這樣或那樣的發現它的問題。此外,它也找不到$
。如果我的代碼示例中沒有足夠的信息,我的代碼全部可用here。
我沒有在任何地方看到對hubs.d.ts的引用,但我可能錯過了一些東西。 –
對不起,最近我拿出來測試,這是在我的打字指數。我正在編輯帖子以包含該信息。 –
我也在爲此苦苦掙扎。但用離子2.你有這個工作嗎? – Mukus