在chrome.storage.local.set中使用变量键 [英] Using a variable key in chrome.storage.local.set
问题描述
我正在创建一个Chrome扩展。
我不知道如何使用变量作为 chrome.storage.local.set()
函数的关键。我试过了
I am creating a chrome-extension.
I don't know how to use a variable as the key to chrome.storage.local.set()
function. I have tried
var key = 'myKey';
chrome.storage.local.get(key, function(val) {
chrome.storage.local.set({key:val[key]+param1}); //appending param1
alert(val[key]);
}
这里我试图获得 val [key]
的值,然后附加一个字符串 param1
并使用相同的密钥将其放回到存储中。
警报框一直显示为undefined。
Here i am trying to get the value of val[key]
and append a string param1
and place it back to the storage using the same key. But I am not able to do so.
the alert box displays undefined all the time.
但是当我尝试不使用变量键
,它可以正常工作。
But when I try without the variable key
, it works fine.
chrome.storage.local.get('myKey', function(val) {
chrome.storage.local.set({myKey:val['myKey']+param1}); //appending param1
alert(val['myKey']);
}
问题是我正在使用 chrome.storage.local.set
不仅可以包含字符串,而且还包含对象的参数。
The problem is that I am using chrome.storage.local.set
can have arguments of not only string but also objects.
推荐答案
我解决了这个问题m。
而不是使用 chrome.storage.local.set({key:val [key] + param1});
I have solved the problem.
Instead of using chrome.storage.local.set({key:val[key]+param1});
我用一个对象替换了json字符串 {key:val [key] + param1}
。
I have replaced the json string {key:val[key]+param1}
with an object.
var obj = {};
var key = "myKey";
obj[key] += param1;
chrome.storage.local.set(obj);
这里 obj
的值 {myKey:附加值}
这篇关于在chrome.storage.local.set中使用变量键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!