繁体   English   中英

将 xterm.js 集成到 Angular

[英]Integrate xterm.js to Angular

我是 Angular 的新手。 我正在尝试使用 xterm.js ( https://xtermjs.org/ ),但显示效果不佳。 这是渲染:渲染

我创建了一个 xterm 组件。 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');
  }
}

我的 xterm.component.html 只包含这个:

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

我真的不知道该怎么做更多......提前致谢伙计们

必须设置组件封装

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

遇到同样的问题,找到了这个页面。 也许这对 OP 来说为时已晚,但对其他人可能有用。

样式错误,因为1) xterm.css没有加载,2)封装。

我对 1) 的解决方案是添加@import 'xterm/dist/xterm.css'; 在此组件的 scss 文件中。

并且 2) 可以通过将encapsulation: ViewEncapsulation.None设置为Victor96 的答案来解决,或者更好地设置encapsulation: ViewEncapsulation.ShadowDom

希望这可以帮助。

尝试通过使用哈希符号在模板引用变量中使用

<div #myTerminal></div>

并在组件中

@ViewChild('myTerminal') terminalDiv: ElementRef;

ngOnInit

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

我知道这很旧,但我不得不将终端初始化放在 ngAfterViewInit 中。 否则 DOM 元素是未定义的。

暂无
暂无

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

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