gpt4 book ai didi

angular - 无法让 Codemirror HTML 语法突出显示在 Angular 2 应用程序中工作

转载 作者:行者123 更新时间:2023-12-02 20:55:41 25 4
gpt4 key购买 nike

尝试在我的 Angular-cli (Angular 2) 应用程序中使 CodeMirror 的 HTML 突出显示正常工作(据我所知,这对某些人来说也是一个问题)。

这是我所拥有的:

app.component.html

<codemirror [(ngModel)]="code" [config]="config"></codemirror>
<div [innerHTML]="code"></div>

app.component.ts

export class AppComponent {
code = `<p>My HTML</p>\n<!-- hello -->\n<p>Your HTML</p>`;

config = {
mode: 'htmlmixed',
lineWrapping: false,
lineNumbers: true,
theme: 'monokai'
};
}

angular-cli.json (仅列出相关部分)

"addons": [
"../node_modules/codemirror/mode/xml/xml.js",
"../node_modules/codemirror/mode/javascript/javascript.js",
"../node_modules/codemirror/mode/htmlmixed/htmlmixed.js"
],

/.../

"styles": [
"../node_modules/codemirror/lib/codemirror.css",
"../node_modules/codemirror/theme/monokai.css"
],

app.module.ts

imports: [
/* ... */
CodemirrorModule,
],

我尝试了各种语法模式,将模式作为对象或字符串传递,并且 - 如您所见 - 确保首先加载 xml。不行——亮点不在那里。我总是确保在每次编辑 angular-cli.json 后重新启动 ngserve

有谁知道问题可能埋在哪里?

最佳答案

您需要添加<script src="node_modules/codemirror/lib/codemirror.js"></script>index.html并且您需要导入 mode你想在你的组件中使用:

import 'codemirror/mode/htmlmixed/htmlmixed'; <-- this

export class AppComponent {
code = `<p>My HTML</p>\n<!-- hello -->\n<p>Your HTML</p>`;

config = {
mode: 'htmlmixed',
lineWrapping: false,
lineNumbers: true,
theme: 'monokai'
};
}

关于angular - 无法让 Codemirror HTML 语法突出显示在 Angular 2 应用程序中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40463724/

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