加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angular – “主题<{}>”类型中不存在属性“地图”

发布时间:2020-12-17 18:11:41 所属栏目:安全 来源:网络整理
导读:所以,我已经看过其他关于类似问题的其他帖子,但没有一个解决方案有帮助. 我也在IntelliJ IDEA 15中进行此开发,而不是Visual Studio.我目前使用的是Typescript 2.0.3. import { Injectable } from '@angular/core';import { Effect,StateUpdates,toPayload }
所以,我已经看过其他关于类似问题的其他帖子,但没有一个解决方案有帮助.

我也在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();
    }
}

解决方法

在您的问题的代码中,您似乎正在执行所需的操作:您已导入主题,并且您已导入/添加了地图运算符.因此,收到TypeScript错误的原因有点神秘.但是,您最近对该问题的编辑确实提出了一种可能性.

很明显,NGRXWebsocketService导入了一个NPM模块:angular2-websocket / angular2-websocket.该模块在rxjs@5.0.0-beta.12上有一个dependency.请注意,这是依赖项而不是peerDependency.

你的node_modules层次结构中有两个独立的rxjs模块是完全可能的:一个在你的app中使用(因此在WebsocketEffects中),另一个在angular2-websocket / angular2-websocket中使用.如果是这种情况,TypeScript会认为getSubject返回的类型与您导入的类型以及添加了map运算符的类型不同(尽管它们都被称为Subject).

您可以通过向WebsocketEffects类添加一些临时代码来验证是否真的如此:

temp() {
    let subject: Subject<any> = this.ngrxWebSocketService.getSubject();
}

如果angular2-websocket / angular2-websocket使用单独的rxjs安装,你应该看到如下错误:

TS2322: Type 'Subject<any>' is not assignable to type 'Subject<any>'.

您还可以运行以下NPM命令以列出node_modules目录下的rxjs安装:

npm list rxjs

多个rxjs模块安装的这种潜力应该由angular2-websocket / angular2-websocket作者解决.但是,如果您确实有多个安装,那么您可以对此进行一些操作(因为rxjs版本相同).如果卸载并重新安装angular2-websocket / angular2-websocket,它应该使用您在应用程序中使用的rxjs模块(而不是自己安装).

事实上,angular2-websocket / angular2-websocket中的所有依赖项应该是对等依赖项而不是依赖项.我建议你把它作为issue.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读