gpt4 book ai didi

javascript - 如何修复vuejs中的 "ReferenceError: require is not defined"

转载 作者:行者123 更新时间:2023-12-02 23:04:52 38 4
gpt4 key购买 nike

我正在开发一个 vuejs + firebase 项目,我试图导入 firestore,但是当我访问我的页面时,这会显示在控制台中:ReferenceError: require is not Define

我尝试将导入部分放入 Mounted() 中,但它说我需要将导入内容放在顶部,这就是我所做的

这是我的 html (register.html) 正文下面:

<script src="https://www.gstatic.com/firebasejs/6.4.0/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/6.4.0/firebase-auth.js"></script>
<script src="https://www.gstatic.com/firebasejs/6.4.0/firebase-firestore.js"></script>

<!-- TODO: Add SDKs for Firebase products that you want to use
https://firebase.google.com/docs/web/setup#config-web-app -->

<script>
// Your web app's Firebase configuration
var firebaseConfig = {
apiKey: "AIzaSyCNYH-sKZZXZhZt5LxQSDqwet7OTo5KgHM",
authDomain: "thelogoagency-3a11d.firebaseapp.com",
databaseURL: "https://thelogoagency-3a11d.firebaseio.com",
projectId: "thelogoagency-3a11d",
storageBucket: "",
messagingSenderId: "101296201242",
appId: "1:101296201242:web:00d0e7e3554b518d"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
</script>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="register.js"></script>

我的register.js 的顶部

// Required for side-effects
require("firebase/firestore");

var app = new Vue({
el: "#root",
data: {
burgerClass: "navbar-burger",
menuClass: "navbar-menu",
mobileMenuIsActive: "",
mobileMenuHasBeenClicked: false,
firstNameInput: "",
lastNameInput: "",
emailInput: "",
passwordInput: "",
authError : "",
emailErrorClass : "",
emailErrorHelp : "",
passwordErrorClass : "",
passwordErrorHelp : "",
inputClass: "input ",
nameErrorHelp: "",
isLoading: false,
firebase: firebase,
db: null,
numberOfInit: 0
},

并安装()

        if(this.numberOfInit = 0)
firebase.initializeApp({
apiKey: 'AIzaSyCNYH-sKZZXZhZt5LxQSDqwet7OTo5KgHM',
authDomain: 'thelogoagency.tk',
projectId: '1:101296201242:web:00d0e7e3554b518d'
});

this.db = firebase.firestore();
this.numberOfInit ++
this.db.collection("users").add({
first: "Ada",
last: "Lovelace",
born: 1815
})
.then(function(docRef) {
console.log("Document written with ID: ", docRef.id);
})
.catch(function(error) {
console.error("Error adding document: ", error);
});

}

它通常应该工作正常,因为它是从 firebase 的文档复制的。我知道我可能做错了:)感谢您的帮助

最佳答案

如果您使用的是 Javascript 而不是 NodeJS,则只需将您的库链接到

<script src="https://my/lib.js"></script>

所以你可以删除你的 require 行:)

关于javascript - 如何修复vuejs中的 "ReferenceError: require is not defined",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57636502/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com