为什么当 pinia 中的状态发生变化时我的 vue 组件不更新?

Why my vue components don't update when the state in pinia changes?

我在一个使用 vue 和 firebase 的网站上工作。在身份验证部分之后,用户进入仪表板,其中有他的项目列表,这些项目是 firestore 中用户文档的子集合。我创建了一个 pinia 商店来管理这些数据,每次使用表单创建项目或删除项目时,state.projects 都会更新新的项目数组,这些项目会循环显示在视图中。在视图中,我可以访问 store.projects,这要归功于 getter,它应该是反应性的,但是当我添加或删除项目时,视图中没有任何反应,但 state.projects 仍然得到更新。

这里是DashboardView.vue的代码:

 <template>
  <MainHeader mode="dashboard" />

  <main class="main">
    <div class="main__container">
      <section class="main__section">
        <div class="section__header">
          <h1 class="header__title">Projects</h1>
          <!-- <TextInput type="text" placeholder="Search" v-model="filter" /> -->
        </div>
        <div class="section__content">
          <ul class="content__list">
            <li
              v-for="project in projects"
              :key="project.id"
              class="content__item"
            >
              {{ project.id }}
              <!-- <router-link
                :to="{ name: 'ProjectView', params: { id: project.id} }">
              </router-link> -->
              <SimpleButton @click="deleteProject(project.id)" type="button" text="delete" />
            </li>
          </ul>
        </div>
        <div class="section__footer">
          <form @submit.prevent="createProject">
            <TextInput type="text" placeholder="name" v-model="form.id" />
            <TextInput type="text" placeholder="website" v-model="form.website" />
            <SimpleButton type="submit" text="Add" />
          </form>
        </div>
      </section>
    </div>
  </main>
</template>

<script>
import { useUserDataStore } from "../stores/UserDataStore.js";
import MainHeader from "../components/MainHeader.vue";
import SimpleButton from "../components/SimpleButton.vue";
import TextInput from "../components/TextInput.vue";
import { ref } from '@vue/reactivity';

export default {
  name: "DashboardView",
  components: {
    MainHeader,
    SimpleButton,
    TextInput,
  },
  setup() {

    // const filter = "";
    const form = ref({});
    const userDataStore = useUserDataStore();
    const projects  = userDataStore.getProjects;

    const createProject = () => {
      userDataStore.createProject(form.value)
    }

    const deleteProject = (id) => {
      userDataStore.deleteProject(id)
    }

    return {
      projects,
      form,
      createProject,
      deleteProject,
    };

  },
};
</script>

这里是 pinia 商店代码:

   import { defineStore } from "pinia";
import router from "../router";
import { db } from '../firebase';
import { doc, setDoc, getDoc, getDocs, collection, deleteDoc } from 'firebase/firestore'

export const useUserDataStore = defineStore('UserDataStore', {
    state: () => {
        userData: { }
        projects: []
        uid: null
    },
    actions: {
        createNewUser(uid, name) {
            setDoc(doc(db, "users", uid), {
                name
            })
                .then(() => {
                    this.fetchUserData(uid)
                })
                .catch((error) => console.log(error))
        },

        fetchUserData(uid) {
            this.uid = uid

            // Fetch user doc with uid
            getDoc(doc(db, "users", uid))
                .then((response) => {
                    this.userData = response.data()

                    // Fetch user projects
                    getDocs(collection(db, "users", uid, "projects"))
                        .then((response) => {

                            const projectsArray = []

                            response.forEach(el => {
                                projectsArray.push({ data: el.data(), id: el.id})
                            })

                            this.projects = projectsArray
                            console.log(this.projects);

                            router.push({ name: 'DashboardView' })
                        })
                })
                .catch((error) => console.log(error))
        },

        createProject(details) {

            const { id, website } = details

            setDoc(doc(db, "users", this.uid, "projects", id), {
                website
            }).then(() => {
                console.log('created');
                this.fetchUserData(this.uid)
            })
            .catch((err) => console.log(err))
        },

        deleteProject(id) {
            deleteDoc(doc(db, "users", this.uid, "projects", id))
                .then(() => {
                    console.log('deleted');
                    this.fetchUserData(this.uid);
                })
                .catch(err => console.log(err))
        }
    },

    getters: {
        getProjects: (state) => state.projects
    }
})

商店是反应性对象,商店 属性 的反应性在设置函数中被访问时被禁用:

const projects  = userDataStore.getProjects;

应该是:

const projects  = computed(() => userDataStore.getProjects);

或者:

const { getProjects: projects } = storeToRefs(userDataStore);