我在main.js中使用Vue.use初始化了全域的SocketIO,在組件中能用sockets里的方法和后臺通信
但是我在一個頁面中引入了多個這個組件,需要每個組件單獨建立一個和后臺的連接,于是我刪掉了全域的Vue.ues,在組件中寫
<script>
import Vue from "vue";
import VueSocketIO from "vue-socket.io";
import SocketIO from "socket.io-client";
Vue.use(
new VueSocketIO({
debug: true,
connection: SocketIO("http://127.0.0.1:5000"),
})
);
export default {
name: "ZuJian",
sockets: {
connect: function () {
console.log(this.$socket);
console.log("client connent socket.id=" + this.$socket.id);
},
}

列印發現每個組件里this.$socket.id相同,相當于多個組件跟后臺還是一個連接。
我想要每個組件中,都是單獨的連接,互不干擾的處理后臺回傳的資料,應該怎么寫呀?
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/255024.html
標籤:JavaScript
