使用 TypeScript 的 socket.io 客户端
我尝试在客户端中将 TypeScript 与 socket.io 一起使用:
import { Manager, Socket} from "socket.io-client";
import {DefaultEventsMap} from "@socket.io/component-emitter";
export class SocketIoService {
private socket: Socket<DefaultEventsMap, DefaultEventsMap>
constructor() {
const manager = new Manager("http://localhost:3000");
this.socket = manager.socket("/");
}
public connect() {
this.socket.emit("hello");
this.socket.on("noArg", () => {
// ...
});
this.socket.on("basicEmit", (a, b, c) => {
// a is inferred as number, b as string and c as buffer
console.log(a + b + c);
});
}
}
问题是当我启动服务器时,出现以下错误:
The requested module './../../../parseuri/index.js' does not provide an export named 'default'
我做错了什么?
I am trying to use TypeScript with socket.io in my client:
import { Manager, Socket} from "socket.io-client";
import {DefaultEventsMap} from "@socket.io/component-emitter";
export class SocketIoService {
private socket: Socket<DefaultEventsMap, DefaultEventsMap>
constructor() {
const manager = new Manager("http://localhost:3000");
this.socket = manager.socket("/");
}
public connect() {
this.socket.emit("hello");
this.socket.on("noArg", () => {
// ...
});
this.socket.on("basicEmit", (a, b, c) => {
// a is inferred as number, b as string and c as buffer
console.log(a + b + c);
});
}
}
The problem is when I start my server I get the following error:
The requested module './../../../parseuri/index.js' does not provide an export named 'default'
What have I done wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
服务器:
客户端:
Server:
Client:
(这应该是一条评论,但我没有足够的代表来发表评论)
该错误表示您正在尝试从已命名导出的模块
默认导入
。你能附上错误的堆栈跟踪吗?
我对触发导入“./../../../parseuri/index.js”的模块感兴趣。
(this should have been a comment but I do not have enough rep to comment)
The error signifies that you are trying to
default import
from a module which has named exports.Can you attach the stacktrace of the error?
I am interested in the module which triggers the import to './../../../parseuri/index.js'.
我不完全确定该错误与此特定代码有何关系。我刚刚创建了一个空文件夹,添加了您的代码,并且使用空的 tsconfig.json 就能够编译。这是编译后的代码:
我建议这样做:
然后再试一次。
I'm not entirely sure how the error is related to this specific code. I just created an empty folder, added your code in, and with an empty
tsconfig.json
was able to compile. Here's the compiled code:I'd suggest doing:
And trying again.