我制作了一个小型应用程序来描述这个问题,我试图使用highlight.js来突出一些代码。问题是,当我从site1按下导航按钮时,它会将我导航到site2,但是高亮显示不适用,如果我从site2到site1,也会发生同样的事情,但是如果我刷新任何页面,它就会生锈。
我需要使用导航,因为它不会丢失变量的值。
testr.ts
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import hljs from 'highlight.js';
@Component({
selector: 'app-testr',
templateUrl: './testr.component.html',
styleUrls: ['./testr.component.css']
})
export class TestrComponent implements OnInit {
constructor(
private router: Router
) {}
ngOnInit() {
hljs.initHighlightingOnLoad();
}
clickme(){
this.router.navigate(['/teste']);
}
}testr.html
<p>testr works!</p>
<pre><code>var char; alert(1+1)</code></pre>
<button (click)="clickme()" value="Link">Link</button>teste.ts
import { Component, OnInit } from '@angular/core';
import hljs from 'highlight.js';
import { Router } from '@angular/router';
@Component({
selector: 'app-teste',
templateUrl: './teste.component.html',
styleUrls: ['./teste.component.css']
})
export class TesteComponent implements OnInit {
constructor(
private router: Router
) { }
ngOnInit() {
hljs.initHighlighting();
}
clickme(){
this.router.navigate(['/testr']);
}
}teste.html
<p>teste works!</p>
<pre><code>var char; alert(1+1)</code></pre>
<button (click)="clickme()" value="Link">Link</button>发布于 2020-10-18 00:14:17
当"onload“事件触发时,Highlight.js不能突出显示不存在的代码。我猜只有第一个视图才存在(页面加载时),而第二个视图只是在“访问”第二个页面时才添加到实际DOM中。
切换页面之后,您需要手动调用initHighlighting来突出显示现在添加到页面中的任何新代码块。
https://stackoverflow.com/questions/59524525
复制相似问题