簡體   English   中英

如何按需加載單獨的文件?

[英]How to load separate file on demand?

我將 Monaco 編輯器用作 JSON 編輯器和模式驗證。 我知道如何在代碼中添加自定義模式驗證。 按照官方文檔: https://microsoft.github.io/monaco-editor/playground.html#extending-language-services-configure-json-defaults

但我想要的是從外部文件加載架構,因此代碼如下所示:

const schema = require('../../../samples/spec-schema.json')

<MonacoEditor height='100%' width='100%'
               language='json'
               theme='vs-dark'
               value={this.state.json}
               onChange={newValue => this.setState(s => Entity(s).set('json', _ => newValue).commit())}
               editorWillMount={monaco => {
                            monaco.languages.json.jsonDefaults.setDiagnosticsOptions({
                                validate: true, 
                                schemas: [{
                                    uri: '../../../samples/spec-schema.json',
                                    schema: schema
                                }]
                            })
                        }}
                    />

我正在使用帶有 Webpack 的 React,我認為需要一種方法來使用 webpack 加載架構,然后將其傳遞給 Monaco 編輯器,但我不知道該怎么做。

我通過啟用enableSchemaRequest: true並在 GitHub Gist 中在線托管我的 JSON 模式解決了這個問題。 只需將$schema: "https://gist.githubusercontent.com/my-schema.json"添加到編輯器頂部,就像在 VS Code 中一樣。

monaco.languages.json.jsonDefaults.setDiagnosticsOptions({
     enableSchemaRequest: true
 })

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM