首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么Highlight.js在重定向之后不能工作?

为什么Highlight.js在重定向之后不能工作?
EN

Stack Overflow用户
提问于 2019-12-30 00:31:17
回答 1查看 274关注 0票数 2

我制作了一个小型应用程序来描述这个问题,我试图使用highlight.js来突出一些代码。问题是,当我从site1按下导航按钮时,它会将我导航到site2,但是高亮显示不适用,如果我从site2到site1,也会发生同样的事情,但是如果我刷新任何页面,它就会生锈。

我需要使用导航,因为它不会丢失变量的值。

testr.ts

代码语言:javascript
复制
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

代码语言:javascript
复制
<p>testr works!</p>
<pre><code>var char; alert(1+1)</code></pre>
<button (click)="clickme()" value="Link">Link</button>

teste.ts

代码语言:javascript
复制
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

代码语言:javascript
复制
<p>teste works!</p>
<pre><code>var char; alert(1+1)</code></pre>
<button (click)="clickme()" value="Link">Link</button>
EN

回答 1

Stack Overflow用户

发布于 2020-10-18 00:14:17

当"onload“事件触发时,Highlight.js不能突出显示不存在的代码。我猜只有第一个视图才存在(页面加载时),而第二个视图只是在“访问”第二个页面时才添加到实际DOM中。

切换页面之后,您需要手动调用initHighlighting来突出显示现在添加到页面中的任何新代码块。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59524525

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档