[Vuejs]-How to import socket.io-client to every component?

0👍

You can make it available for all your components by adding it as an instance property.

For VuejS 3

In your main.js

import { soketio } from 'socket.io-client'

const app = createApp(App)
app.config.globalProperties.$soketio = soketio
app.mount("#app")

For VuejS 2

In your main.js

import { soketio } from 'socket.io-client'

Vue.prototype.$soketio = soketio;
new Vue({ render: h => h(App) }).$mount('#app')

In any component, you can access it using "this", like any other instance property for both Vuejs 2 and Vuejs3

this.$soketio ...

For more details see Vuejs 3.x Global API.

For more details see Vuejs 2.x Instance Properties.

Good luck.

Leave a comment