-
-
Notifications
You must be signed in to change notification settings - Fork 36
Declare for only one component #365
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
Comments
Hi, @Thooto Cool question. This behavior is not supported right now but I think I can add it. The API may look like so: <script>
import io from 'socket.io-client';
const socket = io('http://socketserver.com:1923', { autoConnect: false })
export default {
mixins: [
createSocketIoExtendedMixin(socket)
],
sockets: {
connect() {
console.log('connected');
}
}
mounted: {
this.$socket.connect()
}
}
</script> What do you think about such approach? |
I think that would suit the need perfectly ! (I actually tried something similar using mixins, but couldn't proceed to make it work haha) However as you wrote in example, Thanks for the super quick answer ! |
@Thooto Yeah. Otherwise it will connect before mounting |
Any word on this? I would love this behavior. Or a way to connect and disconnect. |
I ended up importing Vue in the component, making it use VueSocketIo, and exporting <template>
The component template
</template>
<script>
import Vue from "vue";
import VueSocketIo from "vue-socket.io-extended";
import SocketIo from "socket.io-client";
Vue.use(
VueSocketIo,
SocketIo(process.env.VUE_APP_API_URL, {
autoConnect: false
})
);
export default Vue.extend({
data() {
return {};
},
mounted() {
this.$socket.connect();
}
});
</script> Not sure if this is any kind of bad practice, as I didn't check yet if Vue gets loaded twice (making it a heavier app), or if it extends the main Vue module, and then making it possible to potentially use Also disconnection can be done by calling I still would be interested in seeing a mixin approach, as I don't know how I could create a mixin to extend this plugin. :) |
Thanks @Thooto |
It will work also without special mixin: // main.js
import VueSocketIOExt from 'vue-socket.io-extended';
import io from 'socket.io-client';
const socket = io('http://socketserver.com:1923', { autoConnect: false });
Vue.use(VueSocketIOExt, socket); And then in your component: <script>
export default {
sockets: {
connect() {
console.log('connected');
}
},
mounted() {
this.$socket.connect();
},
beforeDestroy() {
this.$socket.disconnect();
}
}
</script> So in this case, during app loading socket connection will be configured but not actually established. It will be waiting for And no, it won't create few instance of Vue or few instances of socket.io - don't worry about that. The purpose of mixin was to declare @Thooto can you confirm that issue is resolved? |
Is this resolved then? @probil I can add this to docs if you want. |
@probil I used the 4 setup lines:
And in a component simply UPDATE: |
Uh oh!
There was an error while loading. Please reload this page.
Hi,
I would need to initiate a socket.io connection only when a component is lazy loaded. (I have an admin section in my UI and I would like the socketio client to not be triggered upon app loading by regular users).
So I would need something equivalent to Vue.use(VueSocketIO) but only for the given component, so that I can still have the sockets: { ... } and this.$socket attributes.
Any idea on how to proceed ? I really can't figure it out ...
Thanks for writing this plugin anyway ! :)
The text was updated successfully, but these errors were encountered: