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

MySQL binlog opening steps

Binlog is a binary log file that is used to recor...

Detailed examples of converting rows to columns and columns to rows in MySQL

mysql row to column, column to row The sentence i...

Use the CSS border-radius property to set the arc

Phenomenon: Change the div into a circle, ellipse...

Detailed process of modifying hostname after Docker creates a container

There is a medicine for regret in the world, as l...

HTML css js implements Tab page sample code

Copy code The code is as follows: <html xmlns=...

Linux CentOS6.5 yum install mysql5.6

This article shares the simple process of install...

Sample code for implementing follow ads with JavaScript

Floating ads are a very common form of advertisin...

Mysql delete data and data table method example

It is very easy to delete data and tables in MySQ...

What to do if the auto-increment primary key in MySQL is used up

In the interview, you should have experienced the...

Vue implements 3 ways to switch tabs and switch to maintain data status

3 ways to implement tab switching in Vue 1. v-sho...

MySQL 8.0.18 installation and configuration method graphic tutorial under MacOS

This article records the installation of MySQL 8....

Detailed analysis of compiling and installing vsFTP 3.0.3

Vulnerability Details VSFTP is a set of FTP serve...

Detailed explanation of Linux host name modification command

Linux change hostname command 1. If you only need...