本文會在宏觀視野上,講述從全棧開發到整棧開發的能力,會展現一個簡單的後臺開發項目片斷,其中採用了element-ui做爲後臺界面開發框架,會分享先後臺通訊之間的API設計經驗,以及vue3.0中使用proxy代理服務解決跨域問題。前端
先建立三個項目目錄,這裏以ele爲例,分別建立前端, 後端, 後臺三個項目。 vue
<div class="login_page fillcontain">
<transition name="form-fade">
<section class="form_container form-fade-enter" v-show="showLogin">
<div class="manage_tip">
<p>elm後臺管理系統</p>
</div>
<el-form :model="loginForm" :rules="rules" ref="loginForm">
<el-form-item prop="username">
<!-- 雙向綁定指令 -->
<el-input v-model="loginForm.username" placeholder="用戶名"></el-input>
</el-form-item>
<el-form-item prop="password">
<!-- 雙向綁定指令 -->
<el-input v-model="loginForm.password" placeholder="密碼" type="password"></el-input>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="submitForm('loginForm')" class="submit_btn">登錄</el-button>
</el-form-item>
</el-form>
</section>
</transition>
</div>
</template>
<script>
export default {
data() {
return {
showLogin: false,
loginForm: {
username: "",
password: ""
},
rules: {
username: [
{ required: true, message: "請輸入用戶名", trigger: "blur" }
],
// 漸進式
password: [
{
required: true,
message: "請輸入密碼",
trigger: "blur"
}
]
}
};
},
mounted() {
this.showLogin = true;
}
};
</script>
<style lang="stylus" scoped>
.login_page {
height: 100vh;
background-color: #324057;
}
.manage_tip {
position: absolute;
width: 100%;
top: -100px;
left: 0;
p {
font-size: 34px;
color: #ffffff;
}
}
.form_container {
width: 320px;
height: 240px;
position: absolute;
top: 50%;
left: 50%;
margin-top: -120px;
margin-left: -160px;
padding: 25px;
box-sizing: border-box;
border-radius: 5px;
text-align: center;
background-color: #ffffff;
.submit_btn {
width: 100%;
font-size: 16px;
}
}
// 動態的產生四個類
.form-fade-enter-active, .form-fade-leave-active {
transition: all 1s;
}
.form-fade-enter, .form-fade-leave-active {
transform: translate3d(0, -50px, 0);
opacity: 0;
}
</style>
複製代碼
transtion是 vue的內置組件,當它的子元素經過生命週期函數改變v-show = "showLogin" false->true的值,從不顯示到顯示,就會觸發,帶來特效。transtion的name屬性會幫咱們動態產生四個類,transition-name-enter(一幀) -> transition-name-enter-active 子元素原來的樣式 transition-name-enter未進入的樣式 transition-name-enter-active 設置transtion-time all 1sajax
methods: {
async submitForm(formName) {
this.$refs[formName].validate(async valid => {
// console.log(valid);
if (valid) {
// 跟後端api通訊吧, 登陸
} else {
this.$notify.error({
title: "錯誤",
message: "請輸入正確的用戶名和密碼",
offset: 100
});
}
});
}
},
複製代碼
$notify 是element-ui中的消息提示組件,至關於iview中的$message. 在Vue中ref至關與id屬性,基於domless的理念,vue中減小dom操做,讓開發更加簡單,用ref代替實現id的功能。express
// api 異步的ajax請求
const res = await login({
user_name: this.loginForm.username,
password: this.loginForm.password
複製代碼
這個login,來自api模塊,爲了不衝突,後端請示都應該在api裏, 這裏須要導入一下element-ui
import { login } from "@/api/getData";
複製代碼
/**
* API定義集合文件,
* @param {*} data
*/
export const login = (data) => {
return new Promise((resolve, reject) => {
// http://127.0.0.1:8080/ 跨項目
fetch('/api/admin/login', {
method: 'POST', //請求頭
body: JSON.stringify(data) //請求體 request body
})
.then(data => data.json())
.then(data => {
console.log(data, '----------')
resolve(data)
})
.catch(err => {
console.log(err)
reject(err)
})
})
}
複製代碼
把能寫的代碼寫完,減小不肯定性json
if (res.status == 1) {
this.$message({
type: "success",
message: "登陸成功"
});
} else {
this.$message({
type: "error",
message: res.message
});
}
複製代碼
這個status,是爲了方便打通先後臺,約定好數據接口,約定後端必定返回一個status: 1|0,來驗證先後端通訊是否通暢。有了這個約定,就能夠不用等到後端項目都寫完,處理前端業務。後端
const express = require('express');
const app = express();
// 路由的模塊化
// api
const router = require('./routes/index.js');
router(app);
// app.use()
app.listen("3000", () => {
console.log('api 服務器上線了');
})
複製代碼
const admin = require('./admin')
module.exports = app => {
// 啓用路由中間件
// 模塊子路由
app.use('/admin', admin)
}
複製代碼
登陸業務忽略api
const express = require('express');
const router = express.Router();
router.post('/login', (req, res) => {
res.send({
status: 1,
message: '登陸成功'
})
})
module.exports = router
複製代碼
vue3.0提供了這個解決方案 在後臺項目根目錄新建一個vue.config.js跨域
module.exports = {
devServer: {
// '127.0.0.1:8080/api/admin/login'
proxy: {
'/api': {
target: 'http://127.0.0.1:3000',
changeOrigin: true,
// http://127.0.0.1:3000/admin/login
pathRewrite: {
'^/api': ''
}
}
}
}
}
複製代碼
經過proxy代理,使用正則速配,若是是/api請求則幫咱們轉發到3000端口下,這樣就不存在跨域問題了。bash