window.showDirectoryPicker() 不允许错误文件选择器已处于活动状态

问题描述 投票:0回答:1
const btn = document.getElementById('btn');
const pre = document.getElementById('pre');

btn.addEventListener('click', async (e) => {

  try {
      const dirHandle = await window.showDirectoryPicker();
      pre.innerHTML = `${dirHandle.name}`;
  } 
  catch (error) {       
      console.log(error);      
  }

});

当我单击“打开”而不选择任何目录并关闭窗口时,此后,当我再次单击按钮(btn)时,它每次都会给出“文件已处于活动状态”并且不会打开目录窗口。有什么解决办法吗?

javascript electron filesystems
1个回答
0
投票

我在这里的意思是,您需要先跟踪窗口,然后您可以检查

folder
file

let isDialogOpen = false;

btn.addEventListener('click', async (e) => {
  if (isDialogOpen) return;

  isDialogOpen = true;
  try {
    const dirHandle = await window.showDirectoryPicker();
    pre.innerHTML = `${dirHandle.name}`;
  } catch (error) {       
      console.log(error);      
  } finally {
     isDialogOpen = false;
  }
});

如果有帮助的话请尝试一下,否则我会尝试其他方法

© www.soinside.com 2019 - 2024. All rights reserved.