我刚刚开始在项目中使用 Laravel(v 8.x)Mix,并且发现从节点模块实现 Javascript 令人沮丧。
首先,我的 webpack.mix.js
中有这个:
mix.js('node_modules/mxgraph/javascript/mxClient.min.js', 'public/js');
mix.js('resources/js/*.js', 'public/js').postCss('resources/css/app.css', 'public/css', [
require('postcss-import'),
require('tailwindcss'),
require('autoprefixer'),
]).version();
接下来,我的 app.js
包含以下内容:
import Canvas from './canvas';
require('mxgraph');
const canvas = new Canvas();
...导入 canvas.js
:
export default class Canvas {
constructor() {
this.container = document.getElementById('graphContainer');
if (!mxClient.isBrowserSupported())
{
// Displays an error message if the browser is not supported.
alert('Browser is not supported!');
}
.
.
.
}
}
...以及我的 Blade 布局的脚本部分:
<script src="{{ mix('js/mxClient.min.js') }}" defer></script>
<script src="{{ mix('js/app.js') }}" defer></script>
当我加载页面时,我在控制台中收到以下错误:
Uncaught ReferenceError: mxClient is not defined
at new Canvas (app.js:3866)
at Module../resources/js/app.js (app.js:3813)
at __webpack_require__ (app.js:114081)
at app.js:114143
at app.js:114149
var mxClient
肯定存在于 mxClient.min.js
中,并且 Canvas
中对它的引用发生在加载之后。
我尝试了很多变体,但似乎没有任何效果。任何指导将不胜感激。
最佳答案
经过多次探索和搜索,我找到了一种可行的方法。它基于我在这里找到的内容:
https://www.programmersought.com/article/85491421858/
我的实现看起来有点笨拙,但它确实有效,而且我现在可以继续尝试使用 mxGraph 开发的项目。
因此,我不再在 webpack.mix.js
中显式引入 mxgraph
,因此它现在恢复为 Laravel 默认值:
mix.js('resources/js/*.js', 'public/js').postCss('resources/css/app.css', 'public/css', [
require('postcss-import'),
require('tailwindcss'),
require('autoprefixer'),
]).version();
我还从 app.js
中删除了 require('mxgraph');
,因此它看起来像这样:
import Canvas from './canvas';
const canvas = new Canvas();
我的 canvas.js
现在看起来像这样:
import mx from 'mxgraph';
const mxgraph = mx({
mxImageBasePath: './src/images',
mxBasePath: './src'
});
window.mxGraph = mxgraph.mxGraph;
window.mxGraphModel = mxgraph.mxGraphModel;
window.mxEvent = mxgraph.mxEvent;
window.mxEditor = mxgraph.mxEditor;
window.mxGeometry = mxgraph.mxGeometry;
window.mxRubberband = mxgraph.mxRubberband;
window.mxDefaultKeyHandler = mxgraph.mxDefaultKeyHandler;
window.mxDefaultPopupMenu = mxgraph.mxDefaultPopupMenu;
window.mxStylesheet = mxgraph.mxStylesheet;
window.mxDefaultToolbar = mxgraph.mxDefaultToolbar;
const {mxGraph, mxClient, mxEvent, mxCodec, mxUtils, mxConstants, mxPerimeter, mxRubberband} = mxgraph;
export default class Canvas {
constructor() {
let container = document.getElementById('graphContainer');
if (typeof(mxClient) !== 'undefined') {
this.draw(container);
}
}
draw (container) {
if (! mxClient.isBrowserSupported())
{
// Displays an error message if the browser is not supported.
mxUtils.error('Browser is not supported!', 200, false);
}
else
{
// Disables the built-in context menu
mxEvent.disableContextMenu(container);
// Creates the graph inside the given container
var graph = new mxGraph(container);
// Enables rubberband selection
new mxRubberband(graph);
// Gets the default parent for inserting new cells. This
// is normally the first child of the root (ie. layer 0).
var parent = graph.getDefaultParent();
// Adds cells to the model in a single step
graph.getModel().beginUpdate();
try
{
var v1 = graph.insertVertex(parent, null, 'Hello,', 20, 20, 80, 30);
var v2 = graph.insertVertex(parent, null, 'World!', 200, 150, 80, 30);
var e1 = graph.insertEdge(parent, null, '', v1, v2);
}
finally
{
// Updates the display
graph.getModel().endUpdate();
}
}
}
}
draw()
方法中的大部分代码取自 mxGraph“Hello World”演示 ( https://jgraph.github.io/mxgraph/docs/manual.html )。
非常感谢@codedge 花时间帮助我!
关于javascript - 通过 Laravel Mix 导入和使用节点模块的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65871988/