Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Can't add event listener at plugin's ready event when upgrade vConsole version to 3.15.0 #591

Closed
johnscliang opened this issue Dec 15, 2022 · 6 comments
Labels

Comments

@johnscliang
Copy link

export default function initHelperPlugin(vconsole, VConsolePlugin) {
  let plugin = new VConsolePlugin('helper', 'Helper');
  plugin.on('renderTab', function(callback) {
    let html = `
    <div>env: ${import.meta.env.MODE}</div>
    <div style='margin-top: 2em;text-align:center'>
      <button id='vconsole_bt_close_vconsole' style='font-size: 32px;'>
      关闭VConsole
      </button>
    </div>
    `
    callback(html);
  });
  plugin.on('ready', function() {
    document.querySelector('#vconsole_bt_close_vconsole').addEventListener('click', function() {
      vconsole.destroy();
    });
  });
  return plugin;
}
document.querySelector('#vconsole_bt_close_vconsole') will come out error with "helper.vconsole.plugin.js?t=1671097953842:15 Uncaught (in promise) TypeError: Cannot read properties of null"
@johnscliang
Copy link
Author

GitHub好卡。。。补充下说明,这个代码在旧版本(3.14.x)是可以的,ready之后可以直接querySelector,升级到最新就不行了。

@johnscliang
Copy link
Author

可以在settimeout中执行没问题,下一个循环对吧。但是这样处理感觉很不爽,请帮忙看看是bug还是我的写法有问题

@johnscliang
Copy link
Author

3.14.6 还可以,3.14.7和之后的版本就会报错。

@Hani1obaid
Copy link

F

@ManiaciaChao
Copy link
Contributor

ManiaciaChao commented Jan 6, 2023

是 bug。从这个提交 #c8d312a 开始,这里不再是同步渲染;但 ready 事件仍然是同步发出的。
在修复之前,需要使用 setTimeout。

johnscliang pushed a commit to johnscliang/vConsole that referenced this issue May 23, 2023
@johnscliang johnscliang mentioned this issue May 23, 2023
Maizify added a commit that referenced this issue May 24, 2023
@Maizify Maizify closed this as completed May 24, 2023
@Maizify Maizify added the Done label May 24, 2023
@Maizify
Copy link
Collaborator

Maizify commented May 24, 2023

当初使用 setTimeout 来渲染插件,使同步变成异步,是存在问题的。
我改了下写法,现在应该是同步的了。

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants