简体   繁体   English

Vue JS:等待Firebase加载

[英]Vue JS : Wait firebase to load

I want to use firebase inside a Vue JS component, but it seems like the firebase object is loaded after the creation of my component. 我想在Vue JS组件中使用firebase,但是好像在创建我的组件之后加载了firebase对象。

Is there a way to wait for firebase to load before executing JS code ? 有没有办法在执行JS代码之前等待Firebase加载?

Exemple : i want to create a vue composant called loader which output the firebase object on the webconsole at his creation 例:我想创建一个名为loader的vue作词器,在创建时将其输出到webconsole上的firebase对象

<html>
<head>

    <!-- Firebase -->
<script defer src="/__/firebase/5.0.4/firebase-app.js"></script>
<!-- include only the Firebase features as you need -->
<script defer src="/__/firebase/5.0.4/firebase-auth.js"></script>
<script defer src="/__/firebase/5.0.4/firebase-database.js"></script>
<script defer src="/__/firebase/5.0.4/firebase-messaging.js"></script>
<script defer src="/__/firebase/5.0.4/firebase-storage.js"></script>
<!-- initialize the SDK after all desired features are loaded -->
<script defer src="/__/firebase/init.js"></script>
<!-- Vue.js-->
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.js"></script>

</head>
<body>
    <div id="app"></div>
<script>
var loader =  {
    template: '<div></div>',
    created: function(){
        console.log(window.firebase); // undefined
    }
};
var app = new Vue({
    el : '#app',
    template : 
        `<div>
            <loader></loader>
        </div>`,
    components : {
        'loader' : loader
    }
});
</script>
</body>
</html>

It doesn't work but, of course, one second later i can output the firebase object on the console. 它不起作用,但是,当然,一秒钟后,我可以在控制台上输出firebase对象。

PS : PS:

  • It is my first question on this website so if you have any advice on how to ask question, i will take it as well. 这是我在该网站上的第一个问题,因此,如果您对如何提出问题有任何建议,我也将接受。
  • I am french so pardon my english 我是法国人,所以请原谅我的英语

You need to remove the defer attributes from the firebase scripts. 您需要从Firebase脚本中删除defer属性。 It will cause the scripts to be executed once the whole document has been parsed, which means that your script in the body runs before firebase scripts. 一旦解析了整个文档,这将导致脚本执行,这意味着您体内的脚本将在firebase脚本之前运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM