Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
616 views
in Technique[技术] by (71.8m points)

nuxtjs + vue-native-websocket how to use?

I have component page. I need connect websocket after page ready;

<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>

Everything connects fine, but if I try use $socket in method, I get error while building :

 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 | 
 

What do I wrong ? The same if I put wss connection in plugin

question from:https://stackoverflow.com/questions/65885065/nuxtjs-vue-native-websocket-how-to-use

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...