首页 > 解决方案 > 如何更新嵌套对象内 LocalStorage 中的某个值?

问题描述

我这样设置:

chrome.storage.local.set({
    myState: {
        data: {
            website: "google.com",
            visited: false,
            count: 0
        }
    }
})

如果我想将 myState.data.visited 更改为 true,我该怎么办?

标签: google-chrome-extensionlocal-storage

解决方案


您需要获取数据,对其进行更改并再次保存。

    chrome.storage.local.get('storageKey', function(result) {
      //data.visited will be in the result object for a specific key. You can change data.visited 
      //to be true here. After changing it to true you can save it again under 
      //the key 'storageKey' or any key you like.

      chrome.storage.local.set({storageKey: result});
    });

推荐阅读