我正试图建立一个页面,有两个JSON代码编辑器并排放置,可以显示两个不同的JSON代码片段,也可以不同的配置,如果需要(即分开的主题,设置,大小和错误)使用ACE编辑器.

到目前为止,我已经算出了大部分工作.然而,我正在try 将相同的代码编辑器共享组件重用为单一父组件中的两个子实例,我希望将这两个实例显示为两个编辑器,显示不同的JSON值和其他参数,这对我来说是行不通的.

以下是代码:

parent.component.html

<div class="projects">
    <!-- layer to display two editors side by side -->
    <div class="row no-margin">
        <div class="col">
            child editor 1
            <childComponent [template]="childTemplate1" jsonValue="{name:joe}" #childEditor1></childComponent>
        </div>
        <div class="col">
            child editor 2
            <childComponent [template]="childTemplate2" jsonValue="{name:bob}" #childEditor2></childComponent>
        </div>
    </div>
</div>

<ng-template #childTemplate1>
    <div class="app-ace-editor" #childEditor1 style="height: 80vh;"></div>
</ng-template>

<ng-template #childTemplate2>
    <div class="app-ace-editor" #childEditor2 style="height: 80vh;"></div>
</ng-template>

parent.component.ts

  @ViewChild('childEditor1') private childEditor1: ElementRef<HTMLElement>;
  @ViewChild('childEditor2') private childEditor2: ElementRef<HTMLElement>;
  @ViewChild(SharedCodeEditorChildComponent) private childEditor: SharedCodeEditorChild;

  constructor() { }

  ngOnInit(): void {
  }

  /* invoke ace editor's method based on the child editor instances */
  ngAfterViewInit(): void {
    this.childEditor.invokeEditor(this.childEditor1);
    this.childEditor.invokeEditor(this.childEditor2);
  }

sharedCodeEditorChild.component.html

<ng-container *ngTemplateOutlet="template"></ng-container>

sharedCodeEditorChild.component.ts

  @Input() template: TemplateRef<any>;
    
  constructor() { }

  ngAfterContentInit(): void {
  
  }

  ngOnInit(): void {
  
  }
  
  ngAfterViewInit(): void {
  
  }

  invokeEditor(elem:any) {
    ace.config.set("fontSize", this.fontSize + 'px');
    ace.config.set('basePath', 'https://unpkg.com/ace-builds@1.4.12/src-noconflict');
    this.aceEditor = ace.edit(elem.nativeElement); // this returns undefined and code further doesnt work
  }

What I tried

我从昨天晚上开始try ,然后用不同的方法在这里提出这个问题,比如:

使用ContentChildrenViewContainerRefViewChildren,甚至使用createEmbeddedView(child.nativeElement)动态设置模板引用.

我试着研究了聊天GPT,堆栈溢出,但不幸的是,没有对我起作用.

What I want

sharedCodeEditorChild.component.ts中,我希望这行this.aceEditor = ace.edit(elem.nativeElement);能够根据我调用的子实例正确地返回子实例的native-element ,这样两个编辑器就可以显示从父实例传递来的两个不同的JSON值,也可以使用单个sharedCodeEditorChildComponent单独配置它们.

如果你能指引我正确的方向,这将是非常有帮助的,也会拯救我的一天.谢谢.

UPDATE:如果任何人正在寻找答案,那么请看看这里https://stackblitz-starters-gpzdau.stackblitz.io,这是现在工作,根据穆利提供的答案在下面.

推荐答案

您在两个地方复制了模板引用#childEditor1#childEditor2!删除此选项并try 初始化

<div class="projects">
    <!-- layer to display two editors side by side -->
    <div class="row no-margin">
        <div class="col">
            child editor 1
            <childComponent [template]="childTemplate1" jsonValue="{name:joe}" #childEditor1></childComponent>
        </div>
        <div class="col">
            child editor 2
            <childComponent [template]="childTemplate2" jsonValue="{name:bob}" #childEditor2></childComponent>
        </div>
    </div>
</div>

<ng-template #childTemplate1>
    <div class="app-ace-editor" #codeEditor1 style="height: 80vh;"></div>
</ng-template>

<ng-template #childTemplate2>
    <div class="app-ace-editor" #codeEditor2 style="height: 80vh;"></div>
</ng-template>

Parent.html

@ViewChild('childEditor1') private childEditor1: SharedCodeEditorChild;
@ViewChild('childEditor2') private childEditor2: SharedCodeEditorChild;

@ViewChild('codeEditor1') private codeEditor1: ElementRef<HTMLElement>;
@ViewChild('codeEditor2') private codeEditor2: ElementRef<HTMLElement>;

  constructor() { }

  ngOnInit(): void {
  }

  /* invoke ace editor's method based on the child editor instances */
  ngAfterViewInit(): void {
    this.childEditor1.invokeEditor(this.codeEditor1);
    this.childEditor2.invokeEditor(this.codeEditor2);
  }

Javascript相关问答推荐

JS、C++和C#给出不同的Base 64 Guid编码结果

*ngFor和@代表输入decorator 和选角闭合

在react js中使用react—router—dom中的Link组件,分配的右侧不能被 destruct ''

如何粗体匹配的字母时输入搜索框使用javascript?

CheckBox作为Vue3中的一个组件

如何在JavaScript文件中使用Json文件

如何通过使用vanilla JS限制字体大小增加或减少两次来改变字体大小

如何在bslib nav_insert之后更改导航标签的CSS类和样式?

在JS中拖放:检测文件

闭包是将值复制到内存的另一个位置吗?

在css中放置所需 colored颜色 以填充图像的透明区域

如何 for each 输入动态设置输入变更值

第一项杀死下一项,直到数组长度在javascript中等于1

有没有一种直接的方法可以深度嵌套在一个JavaScript对象中?

在Vercel中部署Next.js项目时获取`ReferenceError:未定义文档`

$GTE的mongoose 问题

找不到处于状态的联系人

递归地将JSON对象的内容上移一级

如何格式化API的响应

使用JAVASCRIPT-使用If和Else If多次判断条件-使用JAVASRIPT对象及其属性