typescript - this.$socket 在 vue typescript 应用程序中使用 vue-native-websocket 和 vue-socket.io 未定义

标签 typescript vue.js websocket

我使用 Vue Typescript,我想在我的应用程序中使用 WebSocket。我尝试了 view-native-websocketview-socket.io,我对这两个包的相同问题感到困惑。

首先,它们似乎不兼容 typescript 。 (编译器显示消息:“Could not find a declaration file for module 'vue-...”)

但主要问题是在 Vue 组件中 this.$socket 是未定义的,比如 this.$connectionthis.$options.socket,尽管我在 main.ts 中添加了下面的 Vue.use 语句,如文档中所示。

import VueNativeSock from 'vue-native-websocket' Vue.use(VueNativeSock,'ws://localhost:8999');

谁能给我解释一下为什么?

最佳答案

我发现以这种方式访问​​它是可行的:Vue.prototype.$socket

关于typescript - this.$socket 在 vue typescript 应用程序中使用 vue-native-websocket 和 vue-socket.io 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59294129/

相关文章:

html - 无法绑定(bind)到 'configuration',因为它不是 'ng-material-multilevel-menu' 的已知属性

vuejs2 - 在查询后显示数组的特定元素

vue.js - VueJs 为日期格式创建指令

html - WebSockets 与服务器发送的事件/事件源

javascript - 将数据添加到同一行的 2 列中,数据来自 Javascript Websocket

javascript - 如何在firebase.auth(js,ts)中更新用户电话号码

typescript - vue3中props的定义方式是什么(Typescript)

javascript - VueJS 发出的事件未被同一层次结构中的其他组件捕获

c# - Websocket 服务器,客户端无法握手

angular - 带 Angular 管的 Kilo 格式化程序