javascript Nuxt 3:Firebase:需要提供选项,当不通过源部署到托管,(应用程序/无选项)

wn9m85ua  于 2023-05-12  发布在  Java
关注(0)|答案(1)|浏览(261)

我在plugins文件夹中有一个firebase.client.ts文件:

import { initializeApp } from "firebase/app";
import { getAuth } from 'firebase/auth';

const firebaseConfig = {
  /** Configuration here.. */
};

import { defineNuxtPlugin } from '#app';

export default defineNuxtPlugin((nuxtApp) => {
  // Initialize Firebase
  const app = initializeApp(firebaseConfig);

  // Get Firebase Authentication instance
  const auth = getAuth(app);
  nuxtApp.$auth = auth;
});

nuxt.config.ts中,我有这样一行:

plugins: [
    { src: '~/plugins/firebase.client.ts'},
  ],

但是,当我尝试构建yarn buildyarn preview时。我得到这个错误:

我的代码中可能有什么错误?非常感谢您的回复。谢谢你提前:)

taor4pac

taor4pac1#

下面是我用来处理我自己的Firebase插件的类似方法。注意:确保使用firebase库的页面已禁用ssr
~/plugins/firebase-plugin.ts

import { FirebaseOptions, initializeApp } from "firebase/app";
import {
  FacebookAuthProvider,
  GoogleAuthProvider,
  PopupRedirectResolver,
  getAuth,
  signInWithEmailAndPassword,
  signInWithPopup,
} from "firebase/auth";

export default defineNuxtPlugin((nuxtApp) => {
  if (process.client) {
    const app = initializeApp(firebaseConfig as FirebaseOptions);
    const auth = getAuth(app);

    const googleAuth = new GoogleAuthProvider();
    const facebookAuth = new FacebookAuthProvider();

    return {
      provide: {
        firebase: {
          app,
          auth,

          signInWithEmailAndPassword: async (email: string, password: string) =>
            signInWithEmailAndPassword(auth, email, password),

          signInWithGoogle: async (
            resolver?: PopupRedirectResolver | undefined
          ) => signInWithPopup(auth, googleAuth),

          signInWithFacebook: async (
            resolver?: PopupRedirectResolver | undefined
          ) => signInWithPopup(auth, facebookAuth),
          // ...
        },
      },
    };
  }
});

您可以创建一个可组合对象,以便在客户端呈现的页面上的任何位置公开它
~/composables/useFirebase.ts

export function useFirebase() {
  const { $firebase } = useNuxtApp();

  return {
    ...$firebase,
  };
}

相关问题