Vue storage contains a solution for Boolean values

Vue storage contains a solution for Boolean values

Vue stores storage with Boolean values

I encountered a problem today. I needed to store the true and false values ​​returned by the background in storage, and then use the stored values ​​for logical judgment. However, the judgments were always wrong. After checking the information, I found

When storing data in localstorage, the stored boolean values ​​will become strings instead of the boolean values ​​that were initially stored.

insert image description here

All turned into strings

So how to solve it?

1: A conversion is performed on the front end

if (localStorage.getItem('boolean') == 'true') {
		// Reassign the new value 'Boolean value' = true
}

Or when storing, don't use boolean values ​​to store, use numbers or other values ​​instead, and then make the judgment.

// The value returned by the background is true
if (true) {
	localStorage.setItem('Boolean', 1)
}else {
	localStorage.setItem('boolean', 2)
}
// When needed if (localStorage.getItem('Boolean value') == 1) {
	// Handle the event }else {
	// Handle the event}

A pitfall of storing Boolean values ​​in localstorage

Problem Description

I recently used localstorage to store some shared variables at work, but encountered many problems when storing Boolean values;

In general, access is as follows:

localstorage.setItem('key', value);//save localstorage.getItem('key');//get

However, when storing Boolean data, since the Boolean data stored in localstorage is converted into strings, "true"=true and "false"==false, "true"==false are all displayed as false, resulting in many attempts without finding the problem;

Final solution

When localstorage or sessionstorage stores Boolean data, the data obtained becomes the string 'true' 'false'. It is recommended to set the value to 0 or 1 when storing this type of data, and use Number (localstorage.getItem ('key')) when obtaining the value, and then perform subsequent judgment operations;

The specific code is as follows:

Stored value:

if (this.isChecked) {
      //0: checked
      localStorage.setItem("checked",0);
} else {
       //1: not checked
       localStorage.setItem("checked",1);
}

Value:

getFlag:function(){
    var flag = Number (localStorage.getItem ('checked'));
    if(flag==0){
         this.flag=true;
     }else if(flag==1){
          this.flag=false;
     }
}

Summarize:

Both localStorage and sessionStorage can only store string objects, but cannot directly store arrays or objects commonly used in JS;

You can use the parse and stringify methods provided by the JSON object to convert other data types into strings and then store them in storage.

The code is as follows:

Stored value:

localStorage.setItem("flag_data",JSON.stringify(flagData));

Value:

var flag_data = JSON.parse(localStorage.getItem("flag_data"));

The above is my personal experience. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM.

You may also be interested in:
  • How to monitor changes in localStorage or sessionStorage in a Vue project
  • Vue based on localStorage storage information code example
  • Vue uses localstorage to implement breadcrumbs
  • Vue uses localStorage local cache to implement the function of refreshing the verification code without clearing it
  • How to make localStorage responsive in Vue (thought experiment)
  • Retain search box search content based on Vue sessionStorage

<<:  Introduction to the properties of B-Tree

>>:  How to implement online hot migration of KVM virtual machines (picture and text)

Recommend

Comprehensive website assessment solution

<br />Sometimes you may be asked questions l...

Why Seconds_Behind_Master is still 0 when MySQL synchronization delay occurs

Table of contents Problem Description Principle A...

What qualities should a good advertisement have?

Some people say that doing advertising is like bei...

Implementation of vscode custom vue template

Use the vscode editor to create a vue template, s...

Using CSS3 to achieve transition and animation effects

Why should we use CSS animation to replace JS ani...

How to run top command in batch mode

top command is the best command that everyone is ...

Vue3.0 project construction and usage process

Table of contents 1. Project construction 2: Dire...

Div css naming standards css class naming rules (in line with SEO standards)

There are many tasks to be done in search engine o...

Solutions to Files/Folders That Cannot Be Deleted in Linux

Preface Recently our server was attacked by hacke...

New ways to play with CSS fonts: implementation of colored fonts

What if you designers want to use the font below ...

How to implement distributed transactions in MySQL XA

Table of contents Preface XA Protocol How to impl...

Analysis of rel attribute in HTML

.y { background: url(//img.jbzj.com/images/o_y.pn...

Detailed explanation of the use of Arguments object in JavaScript

Table of contents Preface Basic Concepts of Argum...

Implementation of single process control of Linux C background service program

introduce Usually a background server program mus...