sessionStorage / localStorage / cookie stores are all string type

A problem that I have not paid attention to before is sessionStorage / localStorage / cookiethe type of stored data, and there has never been a problem. Today I finally stepped on the pit.

For example:

sessionStorage.setItem('isRuYi', 1)
sessionStorage.setItem('isRuYi', 0)
sessionStorage.setItem('isRuYi', false)

What is stored here is 1 / 0 / false, but sessionStorage will store them in a string format, and then you can convert it to a digital type by *1 if you want to use it later

let isRuYi = sessionStorage.getItem('isRuYi') 

// 在模板中使用:
{isRuYi * 1 ? '如易' : '小恒'}

Guess you like

Origin blog.csdn.net/xiamoziqian/article/details/127771132