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

使用vite-plugin-monaco-editor时JSON展开/折叠功能失效问题求助

使用vite-plugin-monaco-editor时JSON展开/折叠功能失效问题求助

我最近碰到个头疼的问题:在用vite-plugin-monaco-editor展示JSON内容的时候,展开/折叠功能完全没效果——不管怎么点击折叠箭头,JSON数据都纹丝不动,这导致我没法高效浏览大体积的JSON文件,太影响效率了。

我当前用的是Vite,搭配vite-plugin-monaco-editor,插件配置如下:

plugins: [
    react(),
    eslint(),
    MonacoEditorPlugin({
      languageWorkers: ['json'],
    }),
  ],

对应的React组件代码是这样的:

import MonacoEditor from 'react-monaco-editor';

export const CodeEditor = () => {
 const jsonCode = `{
    "employee": {
    "name": "sonoo",
    "salary": 56000,
    "married": true
}
}`;

  /**
   * formatJSON
   */
  function formatJSON(val: string) {
    try {
      const res = JSON.parse(val);
      return JSON.stringify(res, null, 2);
    } catch {
      const errorJson = {
        error: `非法返回${val}`,
      };
      return JSON.stringify(errorJson, null, 2);
    }
  }

  return (
    <>
      <MonacoEditor
        height="600"
        language="json"
        options={{
          readOnly: true,
          automaticLayout: true,
          folding: true, // Ensure folding is enabled
          scrollBeyondLastLine: false,
          minimap: { enabled: false },
        }}
        value={formatJSON(jsonCode)}
        width="800"
      />
    </>
  );
};

我期望JSON内容能正常渲染,同时展开/折叠功能可以正常工作,有没有大佬能帮我看看问题出在哪?

备注:内容来源于stack exchange,提问作者Yash Bhomle

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.14 10:18:03