【发布时间】:2021-07-20 21:22:39
【问题描述】:
我有一个 Rails 6 应用程序,我通过 yarn 添加了 jstree 库。我有application.js 文件,require 语句在哪里。我想做以下
$('#tree').jstree(); 但这会导致 function jstree undefined 异常。我应该如何要求它?
【问题讨论】:
标签: ruby-on-rails jstree webpacker
我有一个 Rails 6 应用程序,我通过 yarn 添加了 jstree 库。我有application.js 文件,require 语句在哪里。我想做以下
$('#tree').jstree(); 但这会导致 function jstree undefined 异常。我应该如何要求它?
【问题讨论】:
标签: ruby-on-rails jstree webpacker
创建一个新的 Rails 应用:
rails new myapp
cd myapp
安装 jstree 和 jQuery(依赖):
yarn add jstree jquery
创建一个新的控制器和视图:
rails g controller welcome index
启动开发服务器和 Rails 服务器:
./bin/webpack-dev-server
rails s
在packs/application.js:
require('../../../node_modules/jstree/dist/themes/default/style.min.css');
global.$ = require('jquery');
require('jstree');
$(() => {
$('#jstree').jstree();
});
添加一些 HTML 到 welcome#index:
<div id="jstree">
<ul>
<li>Root node 1
<ul>
<li id="child_node_1">Child node 1</li>
<li>Child node 2</li>
</ul>
</li>
<li>Root node 2</li>
</ul>
</div>
访问 http://localhost:3000/welcome/index 以查看 jstree 的运行情况。
HTH
【讨论】:
Still getting the following error: application.js:39 Uncaught TypeError: jquery__WEBPACK_IMPORTED_MODULE_0___default(...)(...).jstree is not a function
require('../../../node_modules/jstree/dist/themes/default/style.min.css'); global.$ = require("jquery"); require('jstree'); global.jQuery = global.$; $(() => { $('#jstree').jstree(); });