如何使用由angular2项目中的绝对类型Github repo提供的ng-stomp typedefinition
这是我的angular2服务:SocketService.ts
/// <reference path="../../../typings/globals/sockjs-client/index.d.ts" />
/// <reference path="../../../typings/globals/ng-stomp/index.d.ts" />
import * as SockJS from 'sockjs-client';
export class SocketService {
URL: string = 'http://127.0.0.1:9080/gs-guide-websocket';
sock: any;
stomp: ngStomp;
handlers = {};
private _opened: boolean = false;
public open(): void {
if (!this._opened) {
//this.sock = new SockJS(this.URL);
this.stomp.connect(this.URL).then(function(frame) {
});
console.log('Inside the call for the Socket connection');
this._opened = true;
}
}
public isOpen(): boolean {
return this._opened;
}
public close(): void {
if (this._opened) {
this.sock.close();
delete this.sock;
this._opened = false;
}
}
}
使用输入法安装我的类型定义: a)Sockjs:index.d.ts
// Generated by typings
// Source: https://raw.githubusercontent.com/DefinitelyTyped/DefinitelyTyped/50f81b23065f5c87b19f250c22979f93e1501cfd/sockjs-client/sockjs-client.d.ts
declare namespace __SockJSClient {
interface BaseEvent extends Event {
type: string;
}
interface OpenEvent extends BaseEvent {}
interface CloseEvent extends BaseEvent {
code: number;
reason: string;
wasClean: boolean;
}
interface MessageEvent extends BaseEvent {
data: string;
}
interface SessionGenerator {
(): string;
}
interface Options {
server?: string;
sessionId?: number | SessionGenerator;
transports?: string | string[]
}
enum State {
CONNECTING = 0, OPEN, CLOSING, CLOSED
}
interface SockJSClass extends EventTarget {
readyState: State;
protocol: string;
url: string;
onopen: (e: OpenEvent) => any;
onclose: (e: CloseEvent) => any;
onmessage: (e: MessageEvent) => any;
send(data: any): void;
close(code?: number, reason?: string): void;
}
}
declare module 'sockjs-client' {
import SockJSClass = __SockJSClient.SockJSClass;
import Options = __SockJSClient.Options;
import State = __SockJSClient.State;
var SockJS: {
new(url: string, _reserved?: any, options?: Options): SockJSClass;
(url: string, _reserved?: any, options?: Options): SockJSClass;
prototype: SockJSClass;
CONNECTING: State;
OPEN: State;
CLOSING: State;
CLOSED: State;
};
export = SockJS;
}
b)ng-stomp:index.d.ts
// Generated by typings
// Source: https://raw.githubusercontent.com/DefinitelyTyped/DefinitelyTyped/0d29dd1f2e1ec36df21568d5f9da1263f3d099d7/ng-stomp/ng-stomp.d.ts
interface ngStomp {
sock:any;
stomp:any;
debug:any;
off: any;
setDebug:(callback:Function)=> void;
connect: (endpoint:string, headers?:Headers)=> angular.IHttpPromise<any>;
disconnect: (callback:()=>void) => angular.IHttpPromise<any>;
subscribe: (destination:string, callback:(payload:string, headers:Headers, res:Function)=>void, headers?:Headers, scope?:any) => any;
unsubscribe: () => any;
send: (destination:string, body:any, headers:Headers)=> any;
}
interface Headers {
[key: string]: any;
}
当我尝试连接服务器时抛出以下错误:
内联模板:6:6引起:无法读取未定义的属性“connect”