如何通过 Yarn 将 JS 插件添加到 Rails 6 应用程序

问题描述

我有一个 Rails 6 应用程序,并且已经使用 yarn 和 webpacker 添加了一些插件。但总是有教程指导。

现在我想添加这个:

yarn add @types/waitme

插件下载到 node_modules/@tpyes/waitme

现在使插件工作的下一步是什么?我知道我必须将它包含在我的 application.js 中,但我想了解它是如何工作的。

现在我的 application.js 看起来像这样:

import Rails from "@rails/ujs"
import * as ActiveStorage from "@rails/activestorage"
import "channels"
import "bootstrap"
import "../stylesheets/application"
import Swal from 'sweetalert2';

Rails.start()
ActiveStorage.start()

$(function() {
})

我使用 import,但下一步是什么?

解决方法

那是一个类型定义,而不是一个库;您需要在定义旁边安装库。

npm install --save waitme

然后像使用任何 jquery 插件一样使用该插件。

$('#elem').waitMe({})