我在我的Angular应用程序中使用Ace Editor。它在这里定义- https://www.npmjs.com/package/ng2-ace-editor
用法:
.html
<ace-editor id="editor" class="form-control" formControlName="answer" [ngClass]="validateField('answer')" [(text)]="text"></ace-editor>.ts
ngAfterViewInit(){
this.editor = ace.edit('editor');
ace.config.set('basePath', '/assets/ui/');
ace.config.set('modePath', '/assets/ui/');
ace.config.set('themePath', '/assets/ui/');
ace.config.setModuleUrl('ace/mode/php_worker','/assets/ui/worker-php.js');
ace.config.setModuleUrl('ace/mode/coffee_worker','/assets/ui/worker-coffee.js');
ace.config.setModuleUrl('ace/mode/css_worker','/assets/ui/worker-css.js');
ace.config.setModuleUrl('ace/mode/javascript_worker','/assets/ui/worker-javascript.js');
ace.config
.setModuleUrl('ace/mode/html_worker','/assets/ui/worker-html.js');
ace.config.setModuleUrl('ace/mode/json_worker','/assets/ui/worker-json.js');
ace.config.setModuleUrl('ace/mode/lua_worker','/assets/ui/worker-lua.js');
ace.config.setModuleUrl('ace/mode/xml_worker','/assets/ui/worker-xml.js');
ace.config.setModuleUrl('ace/mode/xquery_worker','/assets/ui/worker-xquery.js');
this.editor.setTheme('ace/theme/eclipse');
}1)我收到以下错误:
blob:http://localhos…99f9-cccd48bdb093:1 Uncaught DOMException: Failed to execute 'importScripts' on 'WorkerGlobalScope': The script at 'http://localhost:9000/worker-html.js' failed to load. at blob:http://localhost:9000/9446350d-625c-418b-99f9-cccd48bdb093:1:1
为什么会这样呢?
2)我找不到这些worker文件的用途,也找不到它们是从哪里包含的。
发布于 2020-03-27 01:31:47
为了让代码正常工作,我必须做两件事
在代码中,添加路径
ace.config.set('basePath', '/assets/ui/');
ace.config.set('modePath', '/assets/ui/');
ace.config.set('themePath', '/assets/ui/');
ace.config.set('workerPath','/assets/ui/');在angular.json中添加以下代码
"assets": [
"src/assets",
"src/favicon.ico",
{
"glob": "**/*",
"input": "./node_modules/ace-builds/src/",
"output": "/"
}
],
"scripts": [
"./node_modules/ace-builds/src/ace.js",
"./node_modules/ace-builds/src/theme-eclipse.js",
"./node_modules/ace-builds/src/theme-monokai.js",
"./node_modules/ace-builds/src/mode-html.js"
]所有的ace文件都在/node_modules/ace-builds/src/中。glob行使它们在Angular将创建最终构建的outDir中可用。在我的例子中,构建角度代码的位置是../public/ui。所以所有的ace文件实际上都会转到../public/ui/。我使用assets的原因是因为我使用的是Play服务器,它使用路径中的assets访问资源。因此,为了获取文件,它将调用例如localhost:9000/assets/ui/worker-html.js。在play应用程序中,我使用路由将所有/assets/...请求映射到/public/...路径
GET /assets/*file controllers.Assets.versioned(path="/public", file: Asset)发布于 2021-01-14 22:49:23
这是为react-ace计算的,它可能会有点类似于angular为我修复它,
<AceEditor
setOptions={{ useWorker: false }}
...props
/>基本上禁用serviceWorker。添加webpack-resolver会增加文件数量和编译时间。这个thread给了我很大的帮助。
发布于 2020-09-29 15:46:17
需要在angular.json文件中定义输出路径。
"assets": [
"src/assets",
{
"glob": "worker-javascript.js",
"input": "./node_modules/ace-builds/src-min/",
"output": "/"
}
]https://stackoverflow.com/questions/60857214
复制相似问题