是否可以通过 Theme 组件监听用户登录事件

恐怕这是不可能的。

这些属性没有 setter;即使有,您的更改也只会临时应用于第一个窗口。一旦用户访问第二个标签页,数据将基于数据库中存储的内容。主题无法访问后端;它们只能更改前端。

您可以做的是在链接中添加一个哈希值,并像这样进行检查。

import { withPluginApi } from "discourse/lib/plugin-api";
import bootbox from "bootbox";

export default {
  name: "first-login-bootbox",
  initialize() {
    withPluginApi("0.8", api => {
      const user = api.getCurrentUser();
      if (!user) return;

      if (
        !user.read_first_notification &&
        !user.enforcedSecondFactor &&
        !window.location.hash
      ) {
        const text = `Lorem ipsum dolor sit amet <a href="http://localhost:3000/new-topic#some-hash" target="_blank">Link</a>, consectetur adipiscing elit, sed do eiusmod tempor`;
        bootbox.alert(text);
      }
    });
  }
};

我不确定您在帖子中链接到 “/new-topic” 是仅仅作为一个示例,还是您确实想这样做。如果这是您期望的结果,那么您还面临另一个问题。即使带有哈希值的页面上没有显示 bootbox,用户仍然会看到以下内容…

…并且编辑器不会打开,这是有道理的,因为用户在其首次页面浏览时立即开始输入主题是非常出乎意料的。

我可以问问您在这里想要实现什么目标吗?您是想告知用户某些事情吗?

我在其他网站上看到过的一种做法是编辑欢迎消息,但如果这是一个选项,还有其他替代方案。

以下是我的建议:

  1. 创建一个主题,并将您想要的所有信息添加进去
  2. 发布该主题
  3. 在 bootbox 中链接到该主题,并在新标签页中打开该链接。

这样,当用户点击链接时,他们会看到类似以下内容(没有覆盖层)

一旦他们完成该页面的操作,就可以返回到第一个标签页,关闭 bootbox,阅读第一条通知,然后继续使用网站。

这样,您甚至不需要添加或检查哈希值。以下是一个示例片段:

import { withPluginApi } from "discourse/lib/plugin-api";
import bootbox from "bootbox";

export default {
  name: "first-login-bootbox",
  initialize() {
    withPluginApi("0.8", api => {
      const user = api.getCurrentUser();
      if (!user) return;

      if (!user.read_first_notification && !user.enforcedSecondFactor) {
        const text = `Lorem ipsum dolor sit amet <a href="http://my.site.com/pub/bentley-flying-spur-s-production-milestone" target="_blank">Link</a>, consectetur adipiscing elit, sed do eiusmod tempor`;
        bootbox.alert(text);
      }
    });
  }
};