简体   繁体   English

如何在 Rails 6 项目中包含 jquery-ui?

[英]How to include jquery-ui in Rails 6 project?

I'm using Rails 6.0.3.2 and jquery-ui 1.12.我正在使用 Rails 6.0.3.2 和 jquery-ui 1.12。

Still can't make it work: Uncaught TypeError: $(...).datepicker is not a function仍然无法使其工作: Uncaught TypeError: $(...).datepicker is not a function

Tried:试过:

I. Editing config/webpack/environment.js一、编辑config/webpack/environment.js

Last version:最后一个版本:

environment.plugins.prepend('Provide',
  new webpack.ProvidePlugin({
  "$":"jquery",
  "jQuery":"jquery",
  "window.jQuery":"jquery",
  Popper: ['popper.js', 'default']
  }))

II.二、 Importing and requiring:导入和要求:

require( "jquery-ui/ui/widgets/datepicker" );

and

import "jquery-ui/ui/widgets/datepicker"

I know there's a lot of questions like mine.我知道有很多像我这样的问题。 However, most of them are obsolete.但是,它们中的大多数已经过时了。 Tried a million of combinations with the same answer: "NOT A FUNCTION"尝试了一百万种具有相同答案的组合:“不是功能”

Please, help with a working config and import/require instructions!请帮助工作配置和导入/需要说明! Thanks in advance!提前致谢!

Finally I figured out how to make all that stuff work (I mean bootstrap, jquery-ui and else).最后我想出了如何让所有这些东西工作(我的意思是引导程序,jquery-ui 等等)。 I wish I would spent less than a half of day collecting the recipe by every little drop.我希望我能花不到半天的时间来收集每一滴菜谱。

My config/webpack/environment.js :我的config/webpack/environment.js

const { environment } = require('@rails/webpacker')
//const { VueLoaderPlugin } = require('vue-loader')
//const vue = require('./loaders/vue')

//environment.plugins.prepend('VueLoaderPlugin', new VueLoaderPlugin())
//environment.loaders.prepend('vue', vue)
const webpack = require('webpack')

module.exports = environment

I commented out the Vue part in case you prefer smth else.我注释掉了 Vue 部分,以防你更喜欢其他东西。

My application.js :我的application.js

require("@rails/ujs").start();
require("turbolinks").start();
require("@rails/activestorage").start();
require("channels");

import * as $ from 'jquery';
import * as bootstrap from 'bootstrap';
import * as datepicker from 'jquery-ui/ui/widgets/datepicker';

document.addEventListener("turbolinks:load", function() {
  if ($('#termsAcceptBtn').length) {
    $('#termsAcceptBtn').addEventListener("click", function() {
      $('#termsModal').modal('show');
    });
  }
});

$(document).on('turbolinks:load', function() {
  $("#user_birth").datepicker();
});

I hope it helps somebody to save a half a day.我希望它可以帮助某人节省半天。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM