gpt4 book ai didi

javascript - 使 PrismJS 语法突出显示在 Aurelia 中工作

转载 作者:搜寻专家 更新时间:2023-10-30 21:46:44 25 4
gpt4 key购买 nike

我正在尝试在我的 Aurelia 应用程序(基于 typescript )中添加 PrismJS 作为语法高亮,我已经完成了一半,如下所示

1- 安装 prismjs

yarn add prismjs

2- 添加 css 和代码部分

<template>
<require from="prismjs/themes/prism.css"></require>

<pre><code class="language-sql">${highlightedSQL}</code></pre>
</template>

3-在组件中引入prismjs并调用highlite。

import "prismjs";
import prismsql from "prismjs/components/prism-sql";

let Prism; // very weird, I have to declare a global variable, other wise it won't work(typescript error)
@inject(HttpClient)
export class Detail {

highlight() {
this.highlightedSQL = Prism.highlight(data.sql, Prism.languages.sql, 'sql');
}
}

我收到这个错误

Unhandled rejection TypeError: Cannot read property 'highlight' of undefined

让它发挥作用的正确方法是什么?

最佳答案

我将发表我的评论作为答案只是为了关闭问题。

而不是 import "prismjs";let Prism; 你应该 import Prism from 'prismjs';

关于javascript - 使 PrismJS 语法突出显示在 Aurelia 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53812542/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com