Rails 7 toastr 使用导入映射

发布于 2025-01-10 06:58:47 字数 1422 浏览 2 评论 0原文

我正在尝试使用 toastr 来显示消息。我正在使用rails 7,但我不确定我是否正确导入并实现了它,

import { Controller } from "@hotwired/stimulus";
import { initializeApp } from 'firebase';
import { getMessaging, getToken, onMessage } from "firebase-messaging";
// import { onBackgroundMessage } from "firebase-messaging-sw";
import * as toastr from 'toastr';

 onMessage(messaging, (payload) => {
        alert(`${ payload.notification.title }\n ${ payload.notification.body }`);
        toastr['success']("Message will come here", "Title");
    });

最后一行什么也没有发生。警报按预期显示

pin "toastr", to: "https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.min.js"

这是我在 importmap.rb 中的 pin 语句

  <head>
<title>NitroCabs</title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<%= csrf_meta_tags %>
<%= csp_meta_tag %>
<%= stylesheet_link_tag "tailwind", "inter-font", "data-turbo-track": "reload" %>
<%= stylesheet_link_tag "application", "data-turbo-track": "reload" %>
<%= javascript_importmap_tags %>
<script>
  if ('serviceWorker' in navigator) {
    window.addEventListener('load', () => {
      navigator.serviceWorker.register('/firebase-messaging-sw.js');
  });
  }
</script>
<%= favicon_link_tag asset_path('firebase-logo.png') %>

这是我在 application.html.erb 中的头

Im trying to use toastr to display a message. Im using rails 7 but Im unsure if i have it imported and implemented correctly

import { Controller } from "@hotwired/stimulus";
import { initializeApp } from 'firebase';
import { getMessaging, getToken, onMessage } from "firebase-messaging";
// import { onBackgroundMessage } from "firebase-messaging-sw";
import * as toastr from 'toastr';

 onMessage(messaging, (payload) => {
        alert(`${ payload.notification.title }\n ${ payload.notification.body }`);
        toastr['success']("Message will come here", "Title");
    });

That last line is where nothing happens. the alert shows up as expected

pin "toastr", to: "https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.min.js"

That is my pin statement in importmap.rb

  <head>
<title>NitroCabs</title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<%= csrf_meta_tags %>
<%= csp_meta_tag %>
<%= stylesheet_link_tag "tailwind", "inter-font", "data-turbo-track": "reload" %>
<%= stylesheet_link_tag "application", "data-turbo-track": "reload" %>
<%= javascript_importmap_tags %>
<script>
  if ('serviceWorker' in navigator) {
    window.addEventListener('load', () => {
      navigator.serviceWorker.register('/firebase-messaging-sw.js');
  });
  }
</script>
<%= favicon_link_tag asset_path('firebase-logo.png') %>

That is my head in application.html.erb

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文