2

我正在尝试在我的 Aurelia 应用程序(基于打字稿)中将 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

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

4

1 回答 1

1

生病发表我的评论作为答案,只是为了让问题结束。

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

于 2018-12-29T16:18:22.547 回答