把socket放到最外面
This commit is contained in:
23
src/App.vue
23
src/App.vue
@@ -1,7 +1,26 @@
|
||||
<script setup>
|
||||
import { onLaunch } from "@dcloudio/uni-app";
|
||||
import { watch } from "vue";
|
||||
import websocket from "@/websocket";
|
||||
import useStore from "@/store";
|
||||
import { storeToRefs } from "pinia";
|
||||
const store = useStore();
|
||||
const { user } = storeToRefs(store);
|
||||
|
||||
onLaunch(() => {});
|
||||
watch(
|
||||
() => user.value.id,
|
||||
(newVal) => {
|
||||
const token = uni.getStorageSync("token");
|
||||
if (newVal && token) {
|
||||
websocket.createWebSocket(token, (content) => {
|
||||
uni.$emit("socket-inbox", content);
|
||||
});
|
||||
}
|
||||
},
|
||||
{
|
||||
deep: false, // 如果 user 是一个对象或数组,建议开启
|
||||
immediate: false, // 若想在初始化时立即执行一次回调,可开启。
|
||||
}
|
||||
);
|
||||
</script>
|
||||
|
||||
<style>
|
||||
|
||||
Reference in New Issue
Block a user