我有组件页面。我需要在页面准备好后连接websocket;
<script lang="ts">
import Vue from 'vue'
import VueNativeSock from 'vue-native-websocket'
export default Vue.extend({
data() {
return {}
},
methods : {
senddata() {
this.$socket.sendObj({awesome: 'data'})
},
},
mounted(){
Vue.use(VueNativeSock, 'ws://cm2:3000', {
reconnection: true, // (Boolean) whether to reconnect automatically (false)
reconnectionAttempts: 5, // (Number) number of reconnection attempts before giving up (Infinity),
reconnectionDelay: 2000, // (Number) how long to initially wait before attempting a new (1000)
})
}
})
</script>
一切都连接得很好,但如果我尝试在方法中使用$socket,我在构建时得到错误:
ERROR ERROR in pages/index.vue:20:9 15:42:05
TS2339: Property '$socket' does not exist on type 'CombinedVueInstance<Vue, {}, { senddata(): void; }, unknown, Readonly<Record<never, any>>>'.
18 | senddata() {
19 |
> 20 | this.$socket.sendObj({awesome: 'data'})
| ^^^^^^^
21 |
22 | },
23 |
我做错了什么?如果我把wss连接放到插件
中也是一样的当我回到commonJS时,问题解决了。