最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Integrate xterm.js to Angular - Stack Overflow

programmeradmin2浏览0评论

I'm new to Angular. I'm trying to use xterm.js (/) but it display badly. Here is the render : Render

I created a xterm component. The xtermponent.ts file code is :

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

@Component({
  selector: 'app-xterm',
  templateUrl: './xtermponent.html',
  styleUrls: ['./xtermponent.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 xtermponent.html only contains this :

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

I don't really know what to do more ... Thanks in advance guys

I'm new to Angular. I'm trying to use xterm.js (https://xtermjs.org/) but it display badly. Here is the render : Render

I created a xterm component. The xterm.component.ts file code is :

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 :

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

I don't really know what to do more ... Thanks in advance guys

Share Improve this question edited Nov 16, 2018 at 12:20 Clément Drouin asked Nov 14, 2018 at 20:06 Clément DrouinClément Drouin 3951 gold badge5 silver badges13 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 6

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.

The styles are wrong because 1) the xterm.css is not loaded, and 2) the encapsulation.

My solution to 1) was to add @import 'xterm/dist/xterm.css'; in the scss file for this component.

And 2) can be solved by setting encapsulation: ViewEncapsulation.None as Victor96's answer, or better setting 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 ngOnInit

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. Otherwise the DOM elements are undefined.

发布评论

评论列表(0)

  1. 暂无评论