Home  >  Q&A  >  body text

Is it possible to store images in vuex? Should this be done?

<p>I'm trying to store profile thumbnails for easy access. Since I'm looking to reduce resource consumption by eliminating the need to get the image path every time the user reloads the page. Can we store image data in vuex? If so, should we do it? </p>
P粉701491897P粉701491897397 days ago460

reply all(1)I'll reply

  • P粉521697419

    P粉5216974192023-08-27 16:46:54

    Yes, you can!

    After the image responds, pass the image to URL.createObjectURL() (Documentation). This will cache the image in the browser and provide a local image URL that you can reuse:

    const imageUrl: string = URL.createObjectURL(response.data);
    if (imageUrl) {
         // 使用以下对象更新您的存储:
         { id: imageId, url: imageUrl };
    }

    You can then search by id in the imageUrl[] state where this URL is saved and retrieve the URL again.

    reply
    0
  • Cancelreply