作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在将 Firebase 导入到我的 Sapper 应用程序中,我不希望在服务器上评估导入。如何确保导入仅在客户端?
我正在使用 Sapper 运行 sapper export
生成静态文件。我试过了:
firebase.auth()
和 firebase.firestore()
模块。 client.js
中创建 Firebase 实例.不成功。 stores.js
中创建实例.不成功。 onMount()
中赋值.这导致我必须在不同的 block 范围内工作。感觉有点hacky。 import firebase from 'firebase/app'
const config = {...}
firebase.initializeApp(config);
import firebase from 'firebase'
我没有收到此服务器错误:
@firebase/app:
Warning: This is a browser-targeted Firebase bundle but it appears it is being run in a Node environment. If running in a Node environment, make sure you are using the bundle specified by the "main" field in package.json.
If you are using Webpack, you can specify "main" as the first item in
"resolve.mainFields": https://webpack.js.org/configuration/resolve/#resolvemainfields
If using Rollup, use the rollup-plugin-node-resolve plugin and set "module" to false and "main" to true: https://github.com/rollup/rollup-plugin-node-resolve
<script>
import { auth } from "../firebase";
</script>
最佳答案
所以我在这方面花了太多时间。没有比 onMOunt 更优雅的解决方案了。
但是,我确实意识到 sapper 确实应该用于它的 SSR 功能。我写了一篇关于如何使用 Sapper SSR 和 Cloud Functions 在 Firebase 上进行设置的文章:
https://dev.to/eckhardtd/how-to-host-a-sapper-js-ssr-app-on-firebase-hmb
原始问题的另一个解决方案是通过 src/template.html
将 Firebase CDN 置于全局范围内。文件。
<body>
<!-- The application will be rendered inside this element,
because `app/client.js` references it -->
<div id='sapper'>%sapper.html%</div>
<!-- Sapper creates a <script> tag containing `app/client.js`
and anything else it needs to hydrate the app and
initialise the router -->
%sapper.scripts%
<!-- Insert these scripts at the bottom of the HTML, but before you use any Firebase services -->
<!-- Firebase App (the core Firebase SDK) is always required and must be listed first -->
<script src="https://www.gstatic.com/firebasejs/6.0.4/firebase-app.js"></script>
<!-- Add Firebase products that you want to use -->
<script src="https://www.gstatic.com/firebasejs/6.0.4/firebase-auth.js"></script>
<script src="https://www.gstatic.com/firebasejs/6.0.4/firebase-firestore.js"></script>
</body>
</html>
<script>
import { onMount } from 'svelte';
let database, authentication;
onMount(() => {
database = firebase.firestore();
authentication = firebase.auth();
});
const authHandler = () => {
if (process.browser) {
authentication
.createUserWithEmailAndPassword()
.catch(e => console.error(e));
}
}
</script>
<button on:click={authHandler}>Sign up</button>
关于firebase - 如何仅在 Sapper 的客户端上导入 Firebase?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56315901/
我是一名优秀的程序员,十分优秀!