由于 ngx-monaco-editor 不支持 angular 17,将其换成了 ngx-monaco-editor-v2,却发现无法正常使用,没有出现 monaco-editor,model 的内容也没有显示,Component 代码如下
Component template:
<ng-container *ngIf="model">
<ngx-monaco-editor class="h-100"
(onInit)="onEditorInitialized()"
[options]="{ model, readOnly: true }"></ngx-monaco-editor>
</ng-container>
Component ts:
export class CommitContentComponent implements OnInit {
protected model?: NgxEditorModel;
ngOnInit(): void {
this.model = {
value: 'Hello, World!',
language: 'markdown',
};
}
}
终于解决了!是 baseUrl
引起的,需要将 /assets/monaco-editor/min/vs
改为 /assets
@NgModule({
imports: [
MonacoEditorModule.forRoot({
baseUrl: environment.assets.monacoBase,
})
]
}
const assets = {
monacoBase: '/assets'
};
还有一个地方需要修改,在 angular.json 中将 monaco-editor 的输出路径改为 assets/monaco
即将
{
"glob": "min/**/*",
"input": "node_modules/monaco-editor",
"output": "assets/monaco-editor"
}
改为
{
"glob": "min/**/*",
"input": "node_modules/monaco-editor",
"output": "assets/monaco"
}