Empty Vue 3 reactive object on some pages

浪子不回头ぞ 提交于 2021-02-11 04:31:21

问题


I have an object that I want to access from all my web app pages. I am using Vue 3 reactivity but I have noted that in some instances, I have to refresh the page to get the object.

How do I refactor my code to have the object always? See my code below:

import { reactive, watch } from "vue";
import getSchoolDocuments from "../composables/getSchoolDocuments";
import { projectAuth } from "../firebase/config";

let user = projectAuth.currentUser;

let userId;

if (!user) {
  userId = localStorage.getItem("userId");
} else {
  userId = user.uid;
}

const { error: schoolError, documents: schoolDetails } = getSchoolDocuments(
  "schools",
  userId
);

watch(schoolDetails, (newValue, oldValue) => {
  console.log(oldValue);

  school.id = newValue["id"];
  school.name = newValue["name"];
  school.staff = newValue["staff"];
  school.vehicles = newValue["vehicles"];
  school.contacts = newValue["contacts"];
 
});

export const school = reactive({
  id: "",
  name: "",
  staff: "",
  vehicles: "",
  error: schoolError
});

来源:https://stackoverflow.com/questions/65950533/empty-vue-3-reactive-object-on-some-pages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!