laravel 使用 npm 安装 CKEditor

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36064972/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:25:44  来源:igfitidea点击:

Installing CKEditor with npm

node.jslaravelckeditorgulplaravel-elixir

提问by Hector Ordonez

I am trying to install CKEditor in my project. I am using Laravel.

我正在尝试在我的项目中安装 CKEditor。我正在使用 Laravel。

I know I could download the files but I like making my life difficult and I decided that I want to install CKEditor as a npm dependency.

我知道我可以下载这些文件,但我喜欢让我的生活变得困难,我决定将 CKEditor 作为 npm 依赖项安装。

As stated in their documentation here, I added the package to package.json, like this:

正如这里的文档中所述,我将包添加到 package.json,如下所示:

"dependencies": {
    "jquery": "^1.11.1",
    "bootstrap-sass": "^3.0.0",
    "elixir-coffeeify": "^1.0.3",
    "laravel-elixir": "^4.0.0",
    "font-awesome": "^4.5.0",
    "ckeditor": "^4.5.7"
}

Now I guess I have to require it in my app.coffee, and so I tried:

现在我想我必须在我的 app.coffee 中要求它,所以我尝试了:

window.$ = window.jQuery = require('jquery')
require('bootstrap-sass')
require('ckeditor')

This surely adds the ckeditor.js script to my app.js. However ckeditor seems to have its own dependencies such as config.js or editor.css, and of course the server responds with 404 for those requests.

这肯定会将 ckeditor.js 脚本添加到我的 app.js 中。但是 ckeditor 似乎有自己的依赖项,例如 config.js 或 editor.css,当然服务器会以 404 响应这些请求。

How could I install CKeditor this way?

我怎么能这样安装CKeditor?

Thank you!

谢谢!

回答by f1ames

There is probably a problem with paths to those CKEditor dependencies. I'm not sure if you are using browserify or something different but for example in browserify using require('ckeditor')will result in ckeditor.js(bundled probably with other js files) file loading from same dir as your app.coffeefile while CKEditor dependencies are in node_modules/ckeditor/dir.

这些 CKEditor 依赖项的路径可能存在问题。我不确定您是使用 browserify 还是其他东西,但例如在 browserify 中使用require('ckeditor')将导致ckeditor.js(可能与其他 js 文件捆绑)从与您的app.coffee文件相同的目录加载文件,而 CKEditor 依赖项在node_modules/ckeditor/目录中。

To tell CKEditor from which directory it should load its dependency you may use CKEDITOR_BASEPATH:

要告诉 CKEditor 应该从哪个目录加载其依赖项,您可以使用CKEDITOR_BASEPATH

window.CKEDITOR_BASEPATH = 'node_modules/ckeditor/'
require('ckeditor')

You may see if there is a problem with loading those files using Networktab in Dev console(e.g. F12in Chrome).

您可能会使用开发控制台中的网络选项卡(例如Chrome 中的F12)查看加载这些文件是否存在问题。

Note thatit's not ideal solution for production environment because then you need node_modulesfolder on your server. You should probably consider moving only those dependencies to other folder during building/releasing process (and use CKEDITOR_BASEPATHas before with path to this productionfolder).

请注意,这不是生产环境的理想解决方案,因为这样您就需要node_modules服务器上的文件夹。您可能应该考虑在构建/发布过程中仅将这些依赖项移动到其他文件夹(并像以前一样使用CKEDITOR_BASEPATH和此生产文件夹的路径)。