[英]How to import src module to public folder in react?
As I'm using Firebase Cloud Messaging
in my React
, it requires me to put the firebase-messaging-sw.js
in my public folder.因为我在我的
React
中使用Firebase Cloud Messaging
,所以它要求我将firebase-messaging-sw.js
放在我的公用文件夹中。
In there, it is defining the function for the onBackgroundMessage
, which I want to have an access to my module, and update some of my components in my src.在那里,它为 onBackgroundMessage 定义了
onBackgroundMessage
,我想访问我的模块,并更新我的 src 中的一些组件。
import { toast } from "react-toastify"; // or import my other modules
messaging.onBackgroundMessage(function(payload) {
console.log('Received background message ', payload);
//toast or do some updating my modules
toast(`Message received : ${payload}`) // here comes the error
});
But I got an error of但是我得到了一个错误
Uncaught SyntaxError: Cannot use import statement outside a module (at firebase-messaging-sw.js:3:1)
未捕获的语法错误:无法在模块外使用导入语句(在 firebase-messaging-sw.js:3:1)
How can I import my src module to the public folder?如何将我的 src 模块导入到 public 文件夹?
How should I properly use the onBackgroundMessage
in React?我应该如何在 React 中正确使用
onBackgroundMessage
?
is it normal to import module from src to the public folder?从 src 导入模块到 public 文件夹是否正常?
Use importScripts to import scripts into the worker's scope like firebase-messaging itself.使用importScripts将脚本导入到工作人员的 scope 中,就像 firebase-messaging 本身一样。
importScripts('https://www.gstatic.com/firebasejs/9.2.0/firebase-messaging.js');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.