You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何获取monaco-editor的当前主题?

获取Monaco Editor当前主题的方法

官方公开API里确实没有提供getTheme方法,但有两种可行的方式获取当前主题:

  • 方式一:维护自定义状态(推荐)
    自己声明一个变量,每次调用setTheme时同步更新这个变量,后续直接读取该变量即可。这种方式完全基于公开API,不会受内部实现变更影响:

    // 初始化默认主题(Monaco默认主题为'vs')
    let currentEditorTheme = 'vs';
    
    // 封装主题设置方法,同步更新状态
    function setEditorTheme(themeName) {
      monaco.editor.setTheme(themeName);
      currentEditorTheme = themeName;
    }
    
    // 获取当前主题
    function getEditorTheme() {
      return currentEditorTheme;
    }
    
  • 方式二:访问内部服务属性(不推荐)
    Monaco内部的_themeService维护着当前主题信息,可以通过非公开属性读取,但这种方式依赖内部实现,版本更新后可能失效,需谨慎使用:

    // 获取当前主题名称
    const currentTheme = monaco.editor._themeService._theme.name;
    

内容的提问来源于stack exchange,提问作者superdc

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 22:09:15