首页 > 解决方案 > 未捕获的语法错误:无法在 Angular 和 Firebase 中的模块外部使用导入语句

问题描述

尝试连接 Firebase 推送通知设置时,我在 Angular 中收到此错误。 在此处输入图像描述

import { initializeApp } from 'https://www.gstatic.com/firebasejs/9.1.0/firebase-app.js';
import { getMessaging, isSupported, onBackgroundMessage } from 'https://www.gstatic.com/firebasejs/9.1.0/firebase-messaging-sw.js';
    
    // Initialize the Firebase app in the service worker by passing in the
    // messagingSenderId.
    
    const firebaseApp = initializeApp({
      apiKey: "",
      authDomain: "",
      projectId: "",
      storageBucket: "",
      messagingSenderId: "",
      appId: "",
      measurementId: ""
    });
    
    // Retrieve an instance of Firebase Messaging so that it can handle background
    // messages.
    isSupported().then(isSupported => {
    
      if (isSupported) {
    
        const messaging = getMessaging(app);
    
        onBackgroundMessage(messaging, ({ notification: { title, body, image } }) => {
          self.registration.showNotification(title, { body, icon: image || '/assets/icons/icon-72x72.png' });
        });
    
      }
    
    });

标签: angularfirebase

解决方案


注册服务人员时需要告诉它是一个模块

在 GitHub 上查看这个已解决的问题: https ://github.com/angular/angularfire/issues/3049#issuecomment-963388580


推荐阅读