Bộ nhớ cục bộ cho phép nhà phát triển lưu trữ và truy xuất dữ liệu trong trình duyệt. Dữ liệu được lưu trữ trong bộ nhớ cục bộ sẽ không hết hạn. Điều này có nghĩa là dữ liệu sẽ tồn tại ngay cả khi tab hoặc cửa sổ trình duyệt bị đóng
điều kiện tiên quyết
Bạn phải có hiểu biết cơ bản về JavaScript. Bạn cũng cần một trình chỉnh sửa mã và trình duyệt để kiểm tra dự án. Trong hướng dẫn này, chúng tôi sẽ sử dụng Visual Studio Code và Google Chrome
Lưu trữ cục bộ là gì
Lưu trữ cục bộ là một dạng lưu trữ web lưu trữ dữ liệu trong một thời gian dài. Đây có thể là một ngày, một tuần hoặc thậm chí một năm. Điều này phụ thuộc vào sở thích của nhà phát triển. Điều quan trọng cần lưu ý là bộ nhớ cục bộ chỉ lưu trữ các chuỗi, vì vậy, nếu bạn muốn lưu trữ các đối tượng, danh sách hoặc mảng, bạn phải chuyển đổi chúng thành chuỗi bằng cách sử dụng
window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
6Khi nào nên sử dụng bộ nhớ cục bộ
Bạn chỉ nên sử dụng bộ nhớ cục bộ khi lưu trữ thông tin nhạy cảm. Điều này là do các cá nhân bên thứ ba có thể dễ dàng truy cập thông tin. Bộ nhớ cục bộ có thể giúp lưu trữ dữ liệu tạm thời trước khi được đẩy lên máy chủ. Điều quan trọng là phải xóa bộ nhớ cục bộ sau khi thao tác này hoàn tất
Hạn chế
Những hạn chế chính của lưu trữ cục bộ là
dữ liệu không an toàn
Hoạt động đồng bộ
Dung lượng lưu trữ hạn chế
Các phương thức chính trong lưu trữ cục bộ
Các phương thức chính khi sử dụng bộ nhớ cục bộ là ________ 07, ________ 08, ________ 09, _______ 40 và
const Car = {
brand:"Suzuki",
color:"white",
price:10000
}
window.localStorage.setItem['car', JSON.stringify[Car]];
1Chìa khóa[]
Phương thức này được sử dụng để truy xuất một giá trị/chuỗi từ một vị trí cụ thể. Chỉ mục có thể được chuyển vào hàm
window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
7 dưới dạng tham sốvar answer = localStorage.key[1];
// this statement will retrieve the value of the second item in localStorage.
window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
7 cũng có thể được sử dụng trong câu lệnh lặp để truy xuất tất cả các mục trong bộ nhớ cục bộthiết lập các mục[]
Chức năng này được sử dụng để lưu trữ các mục trong bộ nhớ cục bộ. Một ví dụ về chức năng này được hiển thị bên dưới
window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
Như đã đề cập trước đó, chúng ta phải
const Car = {
brand:"Suzuki",
color:"white",
price:10000
}
window.localStorage.setItem['car', JSON.stringify[Car]];
4 đối tượng trước khi lưu trữ chúng trong bộ lưu trữ cục bộMột ví dụ được phác thảo dưới đây
const Car = {
brand:"Suzuki",
color:"white",
price:10000
}
window.localStorage.setItem['car', JSON.stringify[Car]];
Không xâu chuỗi đối tượng sẽ dẫn đến lỗi
getItem[]
Chức năng này được sử dụng để truy cập hoặc lấy dữ liệu trong bộ nhớ cục bộ. Phương thức này lấy một
const Car = {
brand:"Suzuki",
color:"white",
price:10000
}
window.localStorage.setItem['car', JSON.stringify[Car]];
5 làm tham số. Sau đó, nó trích xuất giá trị cần thiết từ localStorageVí dụ, để lấy đối tượng
const Car = {
brand:"Suzuki",
color:"white",
price:10000
}
window.localStorage.setItem['car', JSON.stringify[Car]];
6 ở trên, chúng ta sẽ sử dụng câu lệnh sauwindow.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
4Câu lệnh trên sẽ trả về một cái gì đó như thế này
window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
5Bạn nên chuyển đổi nó thành một đối tượng bằng cách sử dụng
const Car = {
brand:"Suzuki",
color:"white",
price:10000
}
window.localStorage.setItem['car', JSON.stringify[Car]];
7 để sử dụng nó trong mã của bạnwindow.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
7loại bỏ mục[]
Phương pháp này được sử dụng để xóa một mục khỏi bộ nhớ cục bộ. Phương thức
window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
9 yêu cầu khóa làm tham sốwindow.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
9xa lạ[]
Phương pháp này được sử dụng để xóa tất cả các giá trị được lưu trữ trong bộ nhớ cục bộ. Nó không yêu cầu bất kỳ tham số nào
window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
0Dự án
Bây giờ chúng ta đã tìm hiểu về các chức năng chính của bộ nhớ cục bộ, hãy tạo một ứng dụng web lưu trữ, truy xuất, xóa và xóa các mục khỏi bộ nhớ cục bộ
Tạo một thư mục mới và mở nó trong trình chỉnh sửa mã của bạn. Tạo hai tệp,
const Car = {
brand:"Suzuki",
color:"white",
price:10000
}
window.localStorage.setItem['car', JSON.stringify[Car]];
9 và window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
40. Tệp const Car = {
brand:"Suzuki",
color:"white",
price:10000
}
window.localStorage.setItem['car', JSON.stringify[Car]];
9 sẽ hiển thị trang web cho người dùng, trong khi tệp window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
40 sẽ lưu trữ các chức năng JavaScript của chúng tôi. Các chức năng này sẽ được sử dụng để truy cập các chức năng khác nhau của bộ nhớ cục bộHãy viết mã
const Car = {
brand:"Suzuki",
color:"white",
price:10000
}
window.localStorage.setItem['car', JSON.stringify[Car]];
9 của chúng ta sẽ có một window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
44 và một số window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
45, như hình bên dướiwindow.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
8Khi nhấp vào nút
window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
46, nó sẽ nhận đầu vào của người dùng và chuyển nó đến hàm window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
47 trong tệp window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
40. window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
49 nhận đầu vào của người dùng. Các giá trị này sau đó được chuyển đến đối tượng xe hơi và được lưu trữ trong bộ nhớ cục bộ bằng phương thức window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
50window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
4Tương tự,
window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
51 sẽ gọi hàm window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
52 khi được nhấp vào. Phương thức này tìm nạp các mục từ localStorage bằng hàm getItemwindow.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
53 tạo một thành phần đoạn văn mới trong trang web của chúng tôiwindow.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
54 giúp tạo văn bản sẽ hiển thị cho người dùngNút văn bản sau đó được thêm vào thẻ đoạn bởi
window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
55Các thành phần này sau đó được hiển thị ở một vị trí cụ thể trên trang web bởi
window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
56 và window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
57window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
0window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
58 gọi window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
9. window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
70 này sẽ xóa một giá trị khỏi bộ nhớ cục bộ bằng hàm window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
71window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
1window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
72 gọi window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
73. Phương pháp const Car = {
brand:"Suzuki",
color:"white",
price:10000
}
window.localStorage.setItem['car', JSON.stringify[Car]];
1 được sử dụng để xóa tất cả các giá trị trong bộ nhớ cục bộwindow.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
2Hãy đặt thuộc tính
window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
75 của tất cả các nút khi tải trang webwindow.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
3Đây là tệp
window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
40 với tất cả các chức năngwindow.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
4Như đã trình bày ở trên, các chức năng sẽ chỉ có thể truy cập được sau khi tải xong trang. Điều này được chỉ định bởi phương pháp
window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
77Đảm bảo rằng tệp
window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
40 được tham chiếu trong tệp const Car = {
brand:"Suzuki",
color:"white",
price:10000
}
window.localStorage.setItem['car', JSON.stringify[Car]];
9 bằng cách dán câu lệnh bên dưới vào phần window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
90window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
5Kết quả
Video sau đây cho thấy cách trang web hoạt động
Sự kết luận
Bây giờ bạn đã quen thuộc với các chức năng khác nhau của bộ nhớ cục bộ. Các phương thức chính trong lưu trữ cục bộ là ________ 150, ________ 192, ________ 171 và ________ 194. Cần có
const Car = {
brand:"Suzuki",
color:"white",
price:10000
}
window.localStorage.setItem['car', JSON.stringify[Car]];
5 khi lưu trữ, truy xuất và xóa các mục khỏi bộ lưu trữ cục bộ. Trong trường hợp bạn không hiểu bất kỳ khái niệm nào, vui lòng xem lại các chức năng lưu trữ cục bộ