pinia引入
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
import { createPinia } from 'pinia';
|
||||
const store = createPinia();;
|
||||
export default store;
|
||||
@@ -0,0 +1,51 @@
|
||||
import { defineStore } from 'pinia';
|
||||
import { ref } from 'vue';
|
||||
import { getToken } from '@/common/common.js'
|
||||
import { get_base_url } from '@/api/request.js'
|
||||
import SocketClass from '@/api/socket.js'
|
||||
|
||||
export const useSocketStore = defineStore('socketStore',{
|
||||
state:()=>({
|
||||
SocketObj: null
|
||||
}),
|
||||
getters:{
|
||||
taskSocket(state){
|
||||
return state.SocketObj
|
||||
}
|
||||
},
|
||||
actions:{
|
||||
creatSocket(){
|
||||
if(this.SocketObj && this.SocketObj?.close) {
|
||||
closeSocket()
|
||||
}
|
||||
this.SocketObj = new SocketClass(get_base_url() + '/trastudy/trasoket/open?summary=' + getToken());
|
||||
},
|
||||
closeSocket: () => {
|
||||
if(!this.SocketObj || !this.SocketObj?.close) return
|
||||
this.SocketObj && this.SocketObj?.close?.()
|
||||
},
|
||||
setOnmessageCb: (cb) => {
|
||||
if(!this.SocketObj || !this.SocketObj?.on) return
|
||||
this.SocketObj.on('message', cb)
|
||||
}
|
||||
}
|
||||
})
|
||||
// () => {
|
||||
// const SocketObj = ref()
|
||||
// const creatSocket = () => {
|
||||
// console.log(data)
|
||||
// if(SocketObj.value && SocketObj.value?.close) {
|
||||
// closeSocket()
|
||||
// }
|
||||
// SocketObj.value = new SocketClass(get_base_url() + '/trastudy/trasoket/open?summary=' + getToken());
|
||||
// }
|
||||
// const closeSocket = () => {
|
||||
// if(!SocketObj.value || !SocketObj.value?.close) return
|
||||
// SocketObj.value && SocketObj.value?.close?.()
|
||||
// }
|
||||
// const setOnmessageCb = (cb) => {
|
||||
// if(!SocketObj.value || !SocketObj.value?.on) return
|
||||
// SocketObj.value.on('message', cb)
|
||||
// }
|
||||
// return { SocketObj, creatSocket, closeSocket, setOnmessageCb }
|
||||
// }
|
||||
Reference in New Issue
Block a user