Can key in sessionStorage be a variable value?

in order to reduce the loading time caused by interaction with the background,
now intends to store the objects acquired from the background in sessionStorage temporarily
and then directly extract
from sessionStorage when needed. However, there may be component reuse in the current page. If the key in sessionStorage in
is the same, the value fetched by all components of
will be the same, causing the display to be inconsistent with expectations.
now you want to use the ID of the component as the key, of sessionStorage
, but after debugging several times, the objects you get are all null
sessionStorage. Do you not support variable key?
if possible, what"s wrong with my method?
I would also like to ask this method of caching back-end information. Is there any better choice for
in addition to sessionStorage?

self.warningData = response.data.data;
var data = self.warningData;
var id = self.myInfoData.id;
sessionStorage.setItem(id,data)
Mar.18,2021

key is fine when it is a variable. The problem is that storage (including sessionStorage and localStorage) can only store string data, not js objects and arrays directly. if you need to store an object or array, you can use JSON.stringify to convert it into a string type and store it, read it and then parse it. For example:

var session = {
    'screens' : [],
    'state' : true
};
localStorage.setItem('session', JSON.stringify(session));
var restoredSession = JSON.parse(localStorage.getItem('session'));
console.log(restoredSession);
Menu