【发布时间】:2017-02-09 04:31:20
【问题描述】:
所以,我看到过很多关于类似问题的其他帖子,但没有一个解决方案有帮助。
我也在 IntelliJ IDEA 15 中进行此开发,而不是 Visual Studio。我目前正在使用 Typescript 2.0.3。
import { Injectable } from '@angular/core';
import { Effect, StateUpdates, toPayload } from '@ngrx/effects';
import { Subject } from 'rxjs/Subject';
import 'rxjs/add/operator/mergeMap';
import 'rxjs/add/observable/dom/webSocket'
import 'rxjs/add/operator/map';
import { AppState } from '../reducers';
import { NGRXWebSocketService } from '../shared/ngrxWebsocket.service';
import { BASE_URL } from "../shared/ngrxWebsocket.service";
export interface WebsocketMessage {
data: string
}
@Injectable()
export class WebsocketEffects {
constructor(private updates$:StateUpdates<AppState>,
private ngrxWebSocketService: NGRXWebSocketService) {
}
@Effect() _recieve$ = this.ngrxWebSocketService.getSubject()
.map((websocketUpdate: WebsocketMessage) => {
let message = JSON.parse(websocketUpdate.data);
return{ type: message.action, payload: message.payload}
});
}
编辑 1:
这是 NGRXWebsocketService。
import {Injectable} from '@angular/core';
import {$WebSocket, WebSocketConfig} from 'angular2-websocket/angular2-websocket'
export const BASE_URL = 'ws://' + location.hostname + ':9000/socket';
@Injectable()
export class NGRXWebSocketService {
private socket: $WebSocket;
constructor() {
this.socket = new $WebSocket(BASE_URL);
}
public sendRequest(request) {
return this.socket.send(request);
}
public reconnect() {
this.socket = new $WebSocket(BASE_URL);
}
public getSubject() {
return this.socket.getDataStream();
}
}
【问题讨论】:
-
如果你这样做
getSubject().asObservable().map会发生什么? -
@peeskillet
error TS2339: Property 'map' does not exist on type 'Observable<{}>'. -
@PaulSwetz 我在我的问题中说我没有使用 Visual Studio。
-
只是IDE的问题吗?还是你手动编译的时候得到这个?
-
我可以在 IDE 中很好地运行应用程序。我手动编译时才看到这个问题。