问题描述
我有一个 Rails 6 应用程序,我通过纱线在其中添加了 <div class="myDiv">Some text that you can select</div>
库。我有 jstree
文件,application.js
语句在哪里。我想做以下事情
require
但这会导致 $('#tree').jstree();
异常。我应该如何要求它?
解决方法
创建一个新的 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();
});
向 welcome#index
添加一些 HTML:
<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