[Vuejs]-Use a variable throughout a Vue component?

0👍

Inside the script tag you can import socket-io.client as follows;

<script>
import io from 'socket.io-client';
...

After import you can declare a variable inside data called socket by creating an instance of io

export default {
   data() {
     return {
       socket: io("Your socket url")
...

You can use socket variable lets say in the created() lifecycle method like this for example;

created() {
   this.socket.emit('LOGIN');
   this.socket.on('CREATED', data => {
      console.log(data);
   });
...

Leave a comment