【问题标题】:flatpickr not loading rails 6 + webpackerflatpickr没有加载rails 6 + webpacker
【发布时间】:2021-07-08 09:35:29
【问题描述】:

我正在尝试将 flatpickr 与 Rails 6 一起使用,但由于某种原因它似乎无法正常工作。

我做了以下事情: yarn add flatpickr

在我的文件中,我有以下内容: packs/application.js

// This file is automatically compiled by Webpack, along with any other files
// present in this directory. You're encouraged to place your actual application logic in
// a relevant structure within app/javascript and only use these pack files to reference
// that code so it'll be compiled.

import 'core-js/stable'
import 'regenerator-runtime/runtime'

import Rails from '@rails/ujs';
import Turbolinks from 'turbolinks';
import * as ActiveStorage from '@rails/activestorage';
import 'channels';

Rails.start();
Turbolinks.start();
ActiveStorage.start();

import('jquery');
import('tooltip-js');
import('../components/main.js');
import('../components/lib_ext.js');
import('../components/dialog.js');
import('../components/widgets.js');
import('../components/classes.js');
import('../components/payroll.js');
import('../components/account_context_menu.js');
import('../components/plan_table.js');
import('../components/requirements.js');
import('../components/help.js');
import('../components/help_data.js');
import('../components/scripts.js'); // nav js
import('../components/additional.js');
import('../components/effects.js');
import('../components/consolidated_orders');
import('../components/sales_invoices.js');
import('../components/journals/auto_reversal.js');
import('../components/budgets/new.js');
import('../components/budgets/export.js');
import('../components/new.js');

import('../stylesheets/application.scss');
const images = require.context('../images', true)

// flatpickr
import flatpickr from 'flatpickr'
require("flatpickr/dist/flatpickr.css");

flatpickr("[data-behavior='flatpickr']", {
  altInput: true,
  altFormat: "F j, Y",
  dateFormat: "Y-m-d",
});

在我的表格中,我有以下内容

<%= f.text_field :company_year_end, data: { behavior: 'flatpickr' } %>

这里的结果是选择器根本不显示。查看控制台,我没有看到任何与 flatpickr 相关的错误。

【问题讨论】:

    标签: javascript ruby-on-rails webpack flatpickr


    【解决方案1】:

    知道了,必须使用 turbolinks 加载

    document.addEventListener('turbolinks:load', () => {
      flatpickr("[data-behavior='flatpickr']", {
        altInput: true,
        altFormat: "F j, Y",
        dateFormat: "Y-m-d",
      });
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-01
      • 2020-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-24
      • 1970-01-01
      • 2022-01-06
      相关资源
      最近更新 更多