angular - 无法从 'sockjs-client' 导入( typescript )

标签 angular typescript npm sockjs

我对 Angular 和 TypeScript 非常陌生。

首先我通过 npm 安装:

npm install --save sockjs-client

我试图在 chat.component.ts 中像这样导入:

import * as SockJS from 'sockjs-client';

但我收到此错误:

TS7016: Could not find a declaration file for module 'sockjs-client'. '/home/simon/javaprojs/tour-creator/client/node_modules/sockjs-client/lib/entry.js' implicitly has an 'any' type.   Try npm i --save-dev @types/sockjs-client if it exists or add a new declaration (.d.ts) file containing declare module 'sockjs-client';

之后我按照错误的建议尝试了以下操作:

npm i --save-dev @types/sockjs-client

但这只会导致新的警告:

Warning: /home/simon/javaprojs/tour-creator/client/src/app/components/chat/chat.component.ts depends on 'sockjs-client'. CommonJS or AMD dependencies can cause optimization bailouts. For more info see: https://angular.io/guide/build#configuring-commonjs-dependencies

这是我的组件的完整代码

import {Component, OnInit} from '@angular/core';

import * as Stomp from 'stompjs';
import * as SockJS from 'sockjs-client';

@Component({
  selector: 'app-chat',
  templateUrl: './chat.component.html',
  styleUrls: ['./chat.component.css'],
})
export class ChatComponent implements OnInit {

  constructor() { }

  connect(): void {
    const socket = new SockJS('gs-guide-websocket');
  }

  ngOnInit(): void {
    this.connect();
  }
}

该怎么办?当我启动应用程序时,我得到的只是一个白页和一个控制台错误,指出全局未定义。

最佳答案

请执行以下命令并重试。

npm install --save sockjs-client    
npm install --save @types/sockjs-client

npm audit fix

请添加此声明

declare module 'sockjs-client';

您还可以访问此链接: How to add SockJS into Angular 2 project?

关于angular - 无法从 'sockjs-client' 导入( typescript ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66522227/

相关文章:

angular - 如何在 Angular 中一起使用 *ngIf 和条件要求?

node.js - 在注册表中找不到 "dt~mocha"("npm")

node.js - 错误!请求的依赖类型无效 : alias

javascript - nodejs 加密的 Hmac sha256 base64 和 CryptoJS 不同

angular - 如何以响应方式使用 Angular flex 布局扭曲和居中多个 div

javascript - 基于可观察变量的带有时间参数的setInterval

typescript - 如何获取数组项的类型?

typescript - 在 typescript npm 模块中重新导出第三方定义

node.js - npm 5.8.0 不支持 Ubuntu 19.04 上的 Node.js v10.15.2

angular - 如何遍历 rxjs 过滤器运算符中的子类别