Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vue.js 为什么我能';无法在弹出窗口中立即获得chrome.storage?_Vue.js_Google Chrome Extension_Vuex_Content Script_Google Chrome Storage - Fatal编程技术网

Vue.js 为什么我能';无法在弹出窗口中立即获得chrome.storage?

Vue.js 为什么我能';无法在弹出窗口中立即获得chrome.storage?,vue.js,google-chrome-extension,vuex,content-script,google-chrome-storage,Vue.js,Google Chrome Extension,Vuex,Content Script,Google Chrome Storage,如果我试图从弹出窗口获取chrome.storage.sync,我需要打开它两次以进行更新 这是我用来做这件事的“教程” 我在内容脚本中设置了一些chrome.storage.sync数据,如下所示: chrome.storage.sync.set({key: value}, function() { console.log('Value is set to ' + value); }); chrome.storage.sync.get(['key'], fu

如果我试图从弹出窗口获取chrome.storage.sync,我需要打开它两次以进行更新

这是我用来做这件事的“教程”

我在内容脚本中设置了一些chrome.storage.sync数据,如下所示:

   chrome.storage.sync.set({key: value}, function() {
          console.log('Value is set to ' + value);
   });
chrome.storage.sync.get(['key'], function(result) {
          console.log('Value currently is ' + result);
  });
在这之后,我在init上打开我的弹出窗口,代码如下:

  chrome.storage.sync.get(['key'], function(result) {
          console.log('Value currently is ' + result.key);
  });
这是可行的,但我需要打开弹出窗口两次才能看到数据更新


弹出窗口是vue应用程序,我需要将chrome.storage数据分配给vuex状态,但我真的不知道怎么做。我花了很多天在这上面,最后我没有正确的方法来解决这个问题。

尝试
result
而不是
result。按如下键:

   chrome.storage.sync.set({key: value}, function() {
          console.log('Value is set to ' + value);
   });
chrome.storage.sync.get(['key'], function(result) {
          console.log('Value currently is ' + result);
  });

您需要等待
chrome.storage.sync.set
调用解析,然后才能使用getter

const promisifiedSet = () => new Promise((resolve) => {
  chrome.storage.sync.set(`{key: value}, () => {
    resolve();
  });
});

// Can now use async/await to wait for the set to finish before calling get.
document.addEventListener('DOMContentLoaded', async () => {
  await promisifiedSet();

  chrome.storage.sync.get(['key'], function(result) {
    console.log('Value currently is ' + result.key);
   });
});

你好谢谢你的回复,但是没有这样的问题。正如你们在问题描述中看到的,我可以得到这些数据,但我需要打开弹出窗口两次。第二个问题是将chrome存储分配到vuex状态。听起来像是一个复制品,可能听起来像,但不是。我不使用ajax或任何http请求。我只需要在内容脚本和带有vuex状态的弹出窗口之间使用chrome.storage和分配的chrome存储数据。但我会阅读这个主题,我相信我会在这里找到答案:PIt是关于异步代码的,而不是关于AJAX本身。还有其他相同的线程,如和谢谢。我会在这里给你的评论结果后得到了解这个链接