简体   繁体   English

Rails 6 webpacker。 Js插件不起作用

[英]Rails 6 webpacker. Js plugins not working

I am using Rails 6. I am having problems with getting js plugins working.我正在使用 Rails 6。我在让 js 插件工作时遇到问题。 I am trying to add toastr.js.我正在尝试添加 toastr.js。

This what I have tried这是我尝试过的

yarn add toastr

This is how my application.js file look这是我的 application.js 文件的外观

require("@rails/ujs").start()
require("turbolinks").start()
require("@rails/activestorage").start()
require("channels")
require('bootstrap')
require("jquery-ui")
require("packs/adminlte")
require("bootstrap-datepicker")
require("toastr/toastr")

This is how my environment.js file looks like这就是我的 environment.js 文件的样子

const { environment } = require('@rails/webpacker');

const webpack = require('webpack');

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

module.exports = environment;

If I use cdn or run the js code at toastr/toastr (node modules) in browser console everything works.如果我使用 cdn 或在浏览器控制台中的toastr/toastr (节点模块)上运行 js 代码,一切正常。

What am I missing?我错过了什么?

Follow below steps to use toastr JS in rails app with webpacker:按照以下步骤在带有 webpacker 的 rails 应用程序中使用 toastr JS:

  • Add toastr with yarn: yarn add toastr用纱线添加 toastr: yarn add toastr

  • In your application.js:在你的 application.js 中:

     import toastr from 'toastr'; toastr.options = { "closeButton": true .... add options here ... }; global.toastr = toastr;

OR或者

toastr = require("toastr")

OR或者

import toastr from 'toastr/toastr';

Try import toastr from 'toastr/toastr';尝试import toastr from 'toastr/toastr'; It worked for me.它对我有用。

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

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