简体   繁体   English

Vue 3 + Pinia - 为什么状态不清除?

[英]Vue 3 + Pinia - Why is the State Not Clearing?

I have a Pinia Store, with the below state:我有一个 Pinia 商店,状态如下:

  worklist: ref({}) as Ref<Worklist>,
  worklists: ref([])  as Ref<Worklist[]>,
  worklistMenuItems: ref([]) as Ref<object[]>,

I am trying to update my data table every time a GET fetch method runs but the state does not seem to be updating for some reason.每次运行 GET fetch 方法时,我都尝试更新我的数据表,但由于某种原因,状态似乎没有更新。

getWorklists(): void
  {
    //Clear worklist values from store before repopulating with results from new GET       

    // This does not seem to be working
    //The reactivity appears to be broken for some reason 
    // this.worklist = {id: undefined, worklistName: ''};
    // this.worklists = [];
    // this.worklistMenuItems.splice(0);

    this.clearValues();

    fetch(apiURL + 'Worklists',
    {
      headers:
      {
        "Accept": "application/json",
        "Content-Type": 'application/json'
      }
    })
    .then(response => 
      {
        return response.json();
      })
    .then(data =>
      {
        data.forEach((wl: Worklist) =>
        {
          // this.worklist = new Worklist(wl.id, wl.worklistName);
          // this.worklists.push(this.worklist);
          
          //Dynamically adding items to View Worklist MenuBar Item
          //Adds an icon for each, displays worklist name and creates a page route to /worklistname
          const worklistMenuItem = ref(
            {
              label: wl.worklistName,
              icon:'pi pi-fw pi-file',
              to: `/worklist/${wl.worklistName}`
            }) as Ref<object>;

            this.$patch((state) =>
            {
              state.worklist = new Worklist(wl.id, wl.worklistName);
              state.worklists.push(this.worklist);
              state.worklistMenuItems.push(worklistMenuItem.value);
              console.log("WORKLIST FULL", this.worklist, state.worklist)
            }
          )
          // this.worklistMenuItems.push(worklistMenuItem.value);
        })
      })
      // console.log(this.worklists)
      const x = ref();
      x.value = this.worklists;
      console.log("THIS IS X ", x.value)
      
  },

and a method to clear the reactive values before running the GET each time以及在每次运行 GET 之前清除反应值的方法

  clearValues()
  {
    this.$patch((state) =>
    {
      state.worklist = {id: undefined, worklistName: ''};
      state.worklists.length = 0;
      state.worklistMenuItems.splice(0);
      console.log("WORKLIST SHOULD BE EMPTY", this.worklists, state.worklists)
    }
  )
  },

I have tried all sorts of things now, but cannot seem to get it working.我现在已经尝试了各种各样的东西,但似乎无法让它发挥作用。

When running the GET, the reactivity does not seem to be working.运行 GET 时,反应似乎不起作用。 After logging the values of store.worklists at the beginning of the GET, after attempting to to clear the values, and the end of the GET after re-populating, I am getting the same data inside it.在 GET 开始时记录 store.worklists 的值后,在尝试清除值后,以及在重新填充后 GET 结束后,我在其中得到相同的数据。 It does not seem to clear at any point.它似乎在任何时候都不清楚。

Does any body know why?有没有人知道为什么? I have tried things like store.worklists = [] I have tried using state.$patch and clearing inside of that I have tried storeToRefs and clearing via my component.我尝试过类似 store.worklists = [] 我尝试过使用 state.$patch 并在其中清除我尝试过 storeToRefs 并通过我的组件进行清除。

I am at a loss now and have been going around in circles for days.我现在很茫然,几天来一直在兜圈子。

Any help is hugely appreciated!非常感谢任何帮助!

Is your clearValues function in the store?您的 clearValues 功能在商店中吗? If so, $patch isn't necessary and you should be able to do the following to update your state:如果是这样, $patch 不是必需的,您应该能够执行以下操作来更新您的状态:

actions: {
    clearValues() {
        this.worklists = [];
        this.worklist = {id: undefined, worklistName: ''};
        this.worklistMenuItems.splice(0);
    }
},

If calling it outside the store:如果在商店外调用它:

const worklistStore = useWorklists();

const clearValues = () => {
    worklistStore.$patch((state) => {
        state.worklists = [];
        state.worklist = {id: undefined, worklistName: ''};
        state.worklistMenuItems.splice(0);
    });
};

Also, your pinia state values don't need a ref to be reactive.此外,您的 pinia 状态值不需要 ref 即可反应。

worklist: {},
worklists: [],
worklistMenuItems: []

Edit: Ah if you're using a function setup rather than object, yes keep the ref.编辑:啊,如果您使用的是功能设置而不是对象,请保留参考。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM