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

ember-ace在安装ember-infinity的Ember项目中无法显示求助

Ember-ace与ember-infinity兼容问题排查与解决

原因分析

ember-infinity@2.3.0版本与ember-ace@3.0.0存在兼容性冲突,核心原因是:

  • 旧版ember-infinity对Ember组件生命周期钩子(如didInsertElement)的修改,干扰了ember-ace内部的DOM渲染与编辑器初始化流程;
  • 两者对Ember运行循环(runloop)的处理逻辑不一致,导致ace编辑器的DOM节点未被正确挂载或初始化,且由于错误被静默处理,控制台无报错信息。

兼容方案

方案1:升级ember-infinity到兼容版本

ember-infinity@2.3.0发布时间较早,对Ember 4.x版本的支持不完善。升级到支持Ember 4.x的最新版本(建议>=3.0.0),执行以下命令:

yarn upgrade ember-infinity@latest

升级后重新启动项目,验证编辑器是否正常显示。

方案2:手动初始化Ace编辑器

若无法升级依赖,可绕过ember-ace的封装,直接使用ace-builds原生API手动初始化编辑器:

  1. 在父组件的JavaScript文件中添加初始化逻辑:
import Component from '@glimmer/component';
import { scheduleOnce } from '@ember/runloop';
import ace from 'ace-builds';
import 'ace-builds/src-noconflict/theme-chaos';
import 'ace-builds/src-noconflict/mode-css';

export default class ParentComponent extends Component {
  didInsertElement() {
    super.didInsertElement?.();
    // 在渲染完成后执行初始化
    scheduleOnce('afterRender', this, () => {
      const editorElement = document.getElementById('custom-ace-editor');
      if (editorElement) {
        const editor = ace.edit(editorElement);
        editor.setOptions({
          minLines: 5,
          maxLines: 20,
          theme: 'ace/theme/chaos',
          mode: 'ace/mode/css'
        });
        editor.setValue(this.args.value || '<p>test</p>', 1);
      }
    });
  }
}
  1. 修改模板,替换为原生div容器:
<div id="custom-ace-editor" style="width: 100%; height: auto; min-height: 150px;"></div>

方案3:调整依赖加载顺序

在ember-cli-build.js中强制指定ace-builds优先于ember-infinity加载,避免加载顺序导致的冲突:

'use strict';

const EmberApp = require('ember-cli/lib/broccoli/ember-app');

module.exports = function(defaults) {
  let app = new EmberApp(defaults, {
    app: {
      import: {
        include: [
          'ace-builds/src-noconflict/ace.js',
          'ace-builds/src-noconflict/theme-chaos.js',
          'ace-builds/src-noconflict/mode-css.js'
        ]
      }
    }
  });

  // 确保ember-infinity在ace-builds之后加载
  app.import('node_modules/ember-infinity/dist/index.js');

  return app.toTree();
};

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 06:42:59