简体   繁体   English

有没有更好的方法来延迟加载 q-select?

[英]Is there a better way to lazy load a q-select?

I'm following https://quasar.dev/vue-components/select#lazy-loading I have我正在关注https://quasar.dev/vue-components/select#lazy-loading我有

@filter="filterFn"
@filter-abort="abortFilterFn"

inside the q-select and filterFn is在 q-select 和 filterFn 里面是

     filterFn (val, update, abort) {
        if (options.value !== null) {
          // already loaded
          update()
          return
        }

        setTimeout(() => {
          update(() => {
            options.value = store.vehicles;
          })
        }, 2000)
      },

So when my api gets data back it puts it in my pinia store, which is happening on selecting options from another q-select in a neighboring component.因此,当我的 api 取回数据时,它会将其放入我的 pinia 存储中,这是在从相邻组件中的另一个 q-select 选择选项时发生的。 I am watching that q-select's model and when it changes, I call my api passing it parameters (that I now know because of the selection) and updating the store once I have data.我正在观察 q-select 的 model,当它发生变化时,我调用我的 api 传递参数(我现在知道因为选择)并在我有数据后更新商店。 However I have to wait for that data to come back that's why I'm using the lazy load.但是我必须等待数据返回,这就是我使用延迟加载的原因。 It works, however a setTimeout is not efficient.它可以工作,但是 setTimeout 效率不高。 Sometimes that data is coming back really fast and sometimes there is a load time of a few seconds if there's a lot of options.有时数据返回得非常快,有时如果有很多选项,加载时间只有几秒钟。 And sometimes I don't know how long it will take.有时我不知道要花多长时间。 So if it's longer than 2 seconds that function wont load it into my q-select.因此,如果它超过 2 秒,function 将不会将其加载到我的 q-select 中。 it just shows blank.它只是显示空白。 Yet I don't want to set it higher and have users wait 3 seconds for a call that may only take one second.但我不想将它设置得更高,让用户等待 3 秒来等待可能只需要一秒钟的呼叫。 I'm using a promise and resolve to get the data, how can I lazy load asynchronously?我正在使用 promise 并解析获取数据,我如何才能异步延迟加载?

so in my api I set the data for the q-select as follows:所以在我的 api 中,我将 q-select 的数据设置如下:

case 'getVehicles':
  store.setVehicles(data.result); //switch on caller thats passed in
  break;

in a process data function that is called on the resolve as such:在这样调用解析的过程数据 function 中:

    function retrieveData(input, params, caller) {
      window.electron
        .sendRequest(input, params)
        .then((reply) => {
          processData(reply, caller);
        });
    }

then my store I just set as such然后我的商店我就这样设置了

    setVehicles(data: []) {
      this.vehicles = data;
    },

You can remove the timeout function. It is meant in the example just to simulate a delayed response from an api, but not necessary in real life.您可以删除超时 function。它在示例中只是为了模拟来自 api 的延迟响应,但在现实生活中不是必需的。 You would likely want to return your store data as a computed prop, to properly watch the response from the api if relying on a Pinia store.如果依赖 Pinia 商店,您可能希望将您的商店数据作为计算道具返回,以便正确观察来自 api 的响应。

const myStoreData = computed(() => {
  return store.data
});

filterFn (val, update, abort) {
  if (options.value !== null) {
   update()
    return
 } else {
 update(() => {
  options.value = myStoreData.value;
 })
}},

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

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