使用 Firebase 时将 HTML、JavaScript 和 CSS 分开
Separate HTML, JavaScript and CSS when using Firebase
我写了一些 HTML 代码,我想托管这个包含 JavaScript 和 CSS 的 HTML 页面。主要问题是我想为 JavaScript 和 CSS 创建单独的文件。我知道它在本地主机上是如何工作的,但由于我使用的是 Firebase,所以我不知道如何继续。
P.S:我对 HTML、CSS 和 JavaScript 有先验知识,但对 Firebase 的了解几乎为零。
代码:
<!DOCTYPE html>
<html>
<head>
<title>noname</title>
<!--<link rel="stylesheet" type="text/css"href="C:\first\public\noname.css">-->
<style type="text/css">
input{
resize: none;
border-radius: 5px;
size: 50px;
height: 100%;
}
#main{
text-align: center;
padding: 20px;
border: 5px solid;
width: 50%;
background-color: #c2e5ef;
background-clip: border-box;
}
.button{
color: #fff;
background-color: #3fb2bf;
margin: 10px 2px;
border: none;
font-size: 20px;
width: 100px;
height: 50px;
border-radius: 15px
}
#login-div{
text-align: center;
}
#logout-div{
text-align: center;
}
</style>
</head>
<body>
<div id="main">
<div id="login-div">
<label><b style="font-size: 20px;">Username:<input type="text" name="username" id="username" placeholder="Enter Username"><br>
</label>
<br>
<label>Password:<input type="text" name="password" id="password" placeholder="Enter Password"></label><br>
<input type="button" name="login" value="Login" class="button" onclick="login()">
</div>
<div id="logout-div">
<p>you are logged in</p>
<input type="button" name="logout" value="Logout" class="button" onclick="logout()">
</div>
<script src="https://www.gstatic.com/firebasejs/4.9.0/firebase.js"></script>
<script type="text/javascript">
var config = {
apiKey: "AIzaSyDf4ndBB-CXJwaYVLXXQacFmX3O9eZbUZk",
authDomain: "first-b14b2.firebaseapp.com",
databaseURL: "https://first-b14b2.firebaseio.com",
projectId: "first-b14b2",
storageBucket: "first-b14b2.appspot.com",
messagingSenderId: "103220354303"
};
firebase.initializeApp(config);
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
// User is signed in.
document.getElementById("login-div").style.display="none";
document.getElementById("logout-div").style.display="block";
} else {
// No user is signed in.
document.getElementById("login-div").style.display="block";
document.getElementById("logout-div").style.display="none";
}
});
function login(){
var email=document.getElementById("username").value;
var pass=document.getElementById("password").value;
firebase.auth().signInWithEmailAndPassword(email, pass).catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
window.alert("error");
// ...
});
}
function logout(){
firebase.auth().signOut().then(function() {
// Sign-out successful.
}).catch(function(error) {
// An error happened.
});
}
</script>
</div>
</body>
</html>
Firebase 对此没有任何具体说明。您只需创建三个单独的文件:index.html
、main.js
和 style.css
,然后将它们 link 放在一起。
style.css
最简单,我们就从这里开始吧:
input{
resize: none;
border-radius: 5px;
size: 50px;
height: 100%;
}
#main{
text-align: center;
padding: 20px;
border: 5px solid;
width: 50%;
background-color: #c2e5ef;
background-clip: border-box;
}
.button{
color: #fff;
background-color: #3fb2bf;
margin: 10px 2px;
border: none;
font-size: 20px;
width: 100px;
height: 50px;
border-radius: 15px
}
#login-div{
text-align: center;
}
#logout-div{
text-align: center;
}
接下来我们从 HMTL 中取出 JavaScript 并将其放入 main.js
:
var config = {
apiKey: "AIzaSyDf4ndBB-CXJwaYVLXXQacFmX3O9eZbUZk",
authDomain: "first-b14b2.firebaseapp.com",
databaseURL: "https://first-b14b2.firebaseio.com",
projectId: "first-b14b2",
storageBucket: "first-b14b2.appspot.com",
messagingSenderId: "103220354303"
};
firebase.initializeApp(config);
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
// User is signed in.
document.getElementById("login-div").style.display="none";
document.getElementById("logout-div").style.display="block";
} else {
// No user is signed in.
document.getElementById("login-div").style.display="block";
document.getElementById("logout-div").style.display="none";
}
});
function login(){
var email=document.getElementById("username").value;
var pass=document.getElementById("password").value;
firebase.auth().signInWithEmailAndPassword(email, pass).catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
window.alert("error");
// ...
});
}
function logout(){
firebase.auth().signOut().then(function() {
// Sign-out successful.
}).catch(function(error) {
// An error happened.
});
}
最后只剩下 index.html
,我们在其中为 style.css
和 main.js
添加了两个导入:
<!DOCTYPE html>
<html>
<head>
<title>noname</title>
<link rel="stylesheet" type="text/css"href="style.css">
</head>
<body>
<div id="main">
<div id="login-div">
<label><b style="font-size: 20px;">Username:<input type="text" name="username" id="username" placeholder="Enter Username"><br>
</label>
<br>
<label>Password:<input type="text" name="password" id="password" placeholder="Enter Password"></label><br>
<input type="button" name="login" value="Login" class="button" onclick="login()">
</div>
<div id="logout-div">
<p>you are logged in</p>
<input type="button" name="logout" value="Logout" class="button" onclick="logout()">
</div>
<script src="https://www.gstatic.com/firebasejs/4.9.0/firebase.js"></script>
<script src="main.js"></script>
</div>
</body>
</html>
您会注意到导入仅使用文件名而不是完整路径完成。像这样进行所谓的相对导入允许导入在本地和部署站点时工作。
不要在浏览器中打开磁盘中的文件,因为当您开始添加更高级的代码时,这势必会导致各种问题。而是从命令提示符 运行 firebase serve
在本地提供文件(和 firebase deploy
将其部署到 Firebase 托管)。
我写了一些 HTML 代码,我想托管这个包含 JavaScript 和 CSS 的 HTML 页面。主要问题是我想为 JavaScript 和 CSS 创建单独的文件。我知道它在本地主机上是如何工作的,但由于我使用的是 Firebase,所以我不知道如何继续。
P.S:我对 HTML、CSS 和 JavaScript 有先验知识,但对 Firebase 的了解几乎为零。
代码:
<!DOCTYPE html>
<html>
<head>
<title>noname</title>
<!--<link rel="stylesheet" type="text/css"href="C:\first\public\noname.css">-->
<style type="text/css">
input{
resize: none;
border-radius: 5px;
size: 50px;
height: 100%;
}
#main{
text-align: center;
padding: 20px;
border: 5px solid;
width: 50%;
background-color: #c2e5ef;
background-clip: border-box;
}
.button{
color: #fff;
background-color: #3fb2bf;
margin: 10px 2px;
border: none;
font-size: 20px;
width: 100px;
height: 50px;
border-radius: 15px
}
#login-div{
text-align: center;
}
#logout-div{
text-align: center;
}
</style>
</head>
<body>
<div id="main">
<div id="login-div">
<label><b style="font-size: 20px;">Username:<input type="text" name="username" id="username" placeholder="Enter Username"><br>
</label>
<br>
<label>Password:<input type="text" name="password" id="password" placeholder="Enter Password"></label><br>
<input type="button" name="login" value="Login" class="button" onclick="login()">
</div>
<div id="logout-div">
<p>you are logged in</p>
<input type="button" name="logout" value="Logout" class="button" onclick="logout()">
</div>
<script src="https://www.gstatic.com/firebasejs/4.9.0/firebase.js"></script>
<script type="text/javascript">
var config = {
apiKey: "AIzaSyDf4ndBB-CXJwaYVLXXQacFmX3O9eZbUZk",
authDomain: "first-b14b2.firebaseapp.com",
databaseURL: "https://first-b14b2.firebaseio.com",
projectId: "first-b14b2",
storageBucket: "first-b14b2.appspot.com",
messagingSenderId: "103220354303"
};
firebase.initializeApp(config);
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
// User is signed in.
document.getElementById("login-div").style.display="none";
document.getElementById("logout-div").style.display="block";
} else {
// No user is signed in.
document.getElementById("login-div").style.display="block";
document.getElementById("logout-div").style.display="none";
}
});
function login(){
var email=document.getElementById("username").value;
var pass=document.getElementById("password").value;
firebase.auth().signInWithEmailAndPassword(email, pass).catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
window.alert("error");
// ...
});
}
function logout(){
firebase.auth().signOut().then(function() {
// Sign-out successful.
}).catch(function(error) {
// An error happened.
});
}
</script>
</div>
</body>
</html>
Firebase 对此没有任何具体说明。您只需创建三个单独的文件:index.html
、main.js
和 style.css
,然后将它们 link 放在一起。
style.css
最简单,我们就从这里开始吧:
input{
resize: none;
border-radius: 5px;
size: 50px;
height: 100%;
}
#main{
text-align: center;
padding: 20px;
border: 5px solid;
width: 50%;
background-color: #c2e5ef;
background-clip: border-box;
}
.button{
color: #fff;
background-color: #3fb2bf;
margin: 10px 2px;
border: none;
font-size: 20px;
width: 100px;
height: 50px;
border-radius: 15px
}
#login-div{
text-align: center;
}
#logout-div{
text-align: center;
}
接下来我们从 HMTL 中取出 JavaScript 并将其放入 main.js
:
var config = {
apiKey: "AIzaSyDf4ndBB-CXJwaYVLXXQacFmX3O9eZbUZk",
authDomain: "first-b14b2.firebaseapp.com",
databaseURL: "https://first-b14b2.firebaseio.com",
projectId: "first-b14b2",
storageBucket: "first-b14b2.appspot.com",
messagingSenderId: "103220354303"
};
firebase.initializeApp(config);
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
// User is signed in.
document.getElementById("login-div").style.display="none";
document.getElementById("logout-div").style.display="block";
} else {
// No user is signed in.
document.getElementById("login-div").style.display="block";
document.getElementById("logout-div").style.display="none";
}
});
function login(){
var email=document.getElementById("username").value;
var pass=document.getElementById("password").value;
firebase.auth().signInWithEmailAndPassword(email, pass).catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
window.alert("error");
// ...
});
}
function logout(){
firebase.auth().signOut().then(function() {
// Sign-out successful.
}).catch(function(error) {
// An error happened.
});
}
最后只剩下 index.html
,我们在其中为 style.css
和 main.js
添加了两个导入:
<!DOCTYPE html>
<html>
<head>
<title>noname</title>
<link rel="stylesheet" type="text/css"href="style.css">
</head>
<body>
<div id="main">
<div id="login-div">
<label><b style="font-size: 20px;">Username:<input type="text" name="username" id="username" placeholder="Enter Username"><br>
</label>
<br>
<label>Password:<input type="text" name="password" id="password" placeholder="Enter Password"></label><br>
<input type="button" name="login" value="Login" class="button" onclick="login()">
</div>
<div id="logout-div">
<p>you are logged in</p>
<input type="button" name="logout" value="Logout" class="button" onclick="logout()">
</div>
<script src="https://www.gstatic.com/firebasejs/4.9.0/firebase.js"></script>
<script src="main.js"></script>
</div>
</body>
</html>
您会注意到导入仅使用文件名而不是完整路径完成。像这样进行所谓的相对导入允许导入在本地和部署站点时工作。
不要在浏览器中打开磁盘中的文件,因为当您开始添加更高级的代码时,这势必会导致各种问题。而是从命令提示符 运行
firebase serve
在本地提供文件(和firebase deploy
将其部署到 Firebase 托管)。