Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Mixin.js for vue 3 #306

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ new Vue({
```

##### Using socket.io-client Instance
###### For vue 2
``` javascript
import Vue from 'vue'
import store from './store'
Expand Down Expand Up @@ -84,6 +85,31 @@ new Vue({
}).$mount('#app')
```

###### For vue 3
``` javascript
import Vue from 'vue'
import store from './store'
import App from './App.vue'
import VueSocketIO from 'vue-socket.io'
import SocketIO from 'socket.io-client'

const vueSocket = new VueSocketIO({
debug: true,
connection: SocketIO('http://metinseylan.com:1992', options), //options object is Optional
vuex: {
store,
actionPrefix: "SOCKET_",
mutationPrefix: "SOCKET_"
}
});

createApp(App)
.use(store)
.use(router)
.use(vueSocket)
.mount('#app');
```

**Parameters**|**Type's**|**Default**|**Required**|**Description**
-----|-----|-----|-----|-----
debug|Boolean|`false`|Optional|Enable logging for debug
Expand Down
8 changes: 4 additions & 4 deletions dist/vue-socketio.js

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,18 @@ export default class VueSocketIO {
*/
install(Vue){

const version = Number(Vue.version.split('.')[0])
const version = Number(Vue.version.split('.')[0]);

if (version >= 3) {
Vue.config.globalProperties.$socket = this.io;
Vue.config.globalProperties.$vueSocketIo = this;
Vue.mixin(Mixin(true));
} else {
Vue.prototype.$socket = this.io;
Vue.prototype.$vueSocketIo = this;
Vue.mixin(Mixin(false));
}

Vue.mixin(Mixin);

Logger.info('Vue-Socket.io plugin enabled');

Expand Down
6 changes: 3 additions & 3 deletions src/mixin.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default {
export default (isVue3) => ({

/**
* Assign runtime callbacks
Expand Down Expand Up @@ -39,7 +39,7 @@ export default {
/**
* unsubscribe when component unmounting
*/
beforeDestroy(){
[isVue3 ? 'beforeUnmount' : 'beforeDestroy'](){

if(this.$options.sockets){

Expand All @@ -53,4 +53,4 @@ export default {

}

}
});