简体   繁体   English

将 xterm.js 集成到 Angular

[英]Integrate xterm.js to Angular

I'm new to Angular.我是 Angular 的新手。 I'm trying to use xterm.js ( https://xtermjs.org/ ) but it display badly.我正在尝试使用 xterm.js ( https://xtermjs.org/ ),但显示效果不佳。 Here is the render : Render这是渲染:渲染

I created a xterm component.我创建了一个 xterm 组件。 The xterm.component.ts file code is : xterm.component.ts 文件代码是:

import { Component, OnInit } from '@angular/core';
import { Terminal } from "xterm";

@Component({
  selector: 'app-xterm',
  templateUrl: './xterm.component.html',
  styleUrls: ['./xterm.component.css'],
})
export class XtermComponent implements OnInit {
  public term: Terminal;
  container: HTMLElement;

  constructor() { }

  ngOnInit() {
    this.term = new Terminal();
    this.container = document.getElementById('terminal');
    this.term.open(this.container);
    this.term.writeln('Welcome to xterm.js');
  }
}

My xterm.component.html only contains this :我的 xterm.component.html 只包含这个:

<div id="terminal"></div>

I don't really know what to do more ... Thanks in advance guys我真的不知道该怎么做更多......提前致谢伙计们

You must set the component encapsulation必须设置组件封装

@Component({
  encapsulation: ViewEncapsulation.None,
  ...
})

Encountered the same problem and found this page.遇到同样的问题,找到了这个页面。 Maybe this is too late for the OP, but could be useful for others.也许这对 OP 来说为时已晚,但对其他人可能有用。

The styles are wrong because 1) the xterm.css is not loaded, and 2) the encapsulation.样式错误,因为1) xterm.css没有加载,2)封装。

My solution to 1) was to add @import 'xterm/dist/xterm.css';我对 1) 的解决方案是添加@import 'xterm/dist/xterm.css'; in the scss file for this component.在此组件的 scss 文件中。

And 2) can be solved by setting encapsulation: ViewEncapsulation.None as Victor96's answer , or better setting encapsulation: ViewEncapsulation.ShadowDom .并且 2) 可以通过将encapsulation: ViewEncapsulation.None设置为Victor96 的答案来解决,或者更好地设置encapsulation: ViewEncapsulation.ShadowDom

Hope this helps.希望这可以帮助。

Try to use in template reference variable by using the hash symbol尝试通过使用哈希符号在模板引用变量中使用

<div #myTerminal></div>

and in component并在组件中

@ViewChild('myTerminal') terminalDiv: ElementRef;

In ngOnInitngOnInit

ngOnInit() {
    this.term = new Terminal();
    this.term.open(this.terminalDiv.nativeElement);
    this.term.writeln('Welcome to xterm.js');
  }

I know this is old, but I had to put terminal initialation in ngAfterViewInit.我知道这很旧,但我不得不将终端初始化放在 ngAfterViewInit 中。 Otherwise the DOM elements are undefined.否则 DOM 元素是未定义的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM