简体   繁体   English

如何将 src 模块导入 React 中的公用文件夹?

[英]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.

相关问题 放大 React 构建失败,显示“未找到模块:错误:无法解析‘/codebuild/output/src313005886/src/client-portal/src’中的‘./components/...’” - Amplify React build fail with "Module not found: Error: Can't resolve './components/...' in '/codebuild/output/src313005886/src/client-portal/src' " Django - 如何导入 python 模块? - Django - how to import python module? 未找到模块:错误:您尝试导入项目 src/ 目录之外的 /firebase/app - Module not found: Error: You attempted to import /firebase/app which falls outside of the project src/ directory 如何在导入时为模块分配别名? - How to assign an alias to a module on import? ./src/index.js 未找到模块:无法解析“aws-amplify”React - ./src/index.js Module not found: Can't resolve 'aws-amplify' React 在 Firebase 存储中公开文件夹 - making a folder public in Firebase Storage 如何导入 firebase 应用程序作为浏览器模块导入? - How can I import firebase app as browser module import? 如何在普通 HTML 中导入 Javascript 模块? - How to import a Javascript module in plain HTML? 如何在 Nodejs 中使用 pako 模块压缩目录/文件夹? - How to gzip a directory/folder using pako module in Nodejs? 如何列出 firebase 文件夹中的所有项目? - How to listAll items inside firebase folder react?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM