安裝vue-cli
npm install @vue/cli -g
創(chuàng)建項(xiàng)目
vue create myapp
選擇 Manually select features
1.png
image.png
選擇 node-sass 后面的樣式就可以使用scss
image.png
安裝并引入 element ui
npm install element-ui --save
編輯 src/main.js , 修改為
import Vue from 'vue'
import App from './App'
import router from './router'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
Vue.use(ElementUI)
Vue.config.productionTip = false
new Vue({
el: '#app',
router,
render: h => h(App)
})
編寫(xiě)登錄頁(yè)面
src/views/Login.vue
<template>
<div class="login-container">
<el-form :model="myForm" :rules="myRule"
status-icon
ref="myForm"
label-position="left"
label-width="0px"
class="demo-ruleForm login-page">
<h3 class="title">登錄</h3>
<el-form-item prop="username">
<el-input type="text"
v-model="myForm.username"
auto-complete="off"
placeholder="用戶(hù)名"
/>
</el-form-item>
<el-form-item prop="password">
<el-input type="password"
v-model="myForm.password"
auto-complete="off"
placeholder="密碼"
/>
</el-form-item>
<el-checkbox
v-model="checked"
class="rememberme"
>記住密碼</el-checkbox>
<el-form-item style="width:100%;">
<el-button type="primary" style="width:100%;" @click="handleSubmit" :loading="logining">登錄</el-button>
</el-form-item>
</el-form>
</div>
</template>
<script>
export default {
data(){
return {
logining: false,
myForm: {
username: 'admin',
password: '123456',
},
myRule: {
username: [{required: true, message: '請(qǐng)輸入賬號(hào)', trigger: 'blur'}],
password: [{required: true, message: '請(qǐng)輸入密碼', trigger: 'blur'}]
},
checked: false
}
},
methods: {
handleSubmit () {
this.$refs.myForm.validate((valid) => {
if (valid) {
this.logining = true;
if (this.myForm.username === 'admin' && this.myForm.password === '123456') {
this.logining = false;
sessionStorage.setItem('user', this.myForm.username);
this.$router.push({path: '/'});
}else{
this.logining = false;
this.$alert('賬號(hào)或密碼錯(cuò)誤', '溫馨提示', {
confirmButtonText: '確定'
})
}
}else{
console.log('error submit!');
return false;
}
})
}
}
};
</script>
<style>
.login-container {
width: 100%;
height: 100%;
}
.login-page {
-webkit-border-radius: 5px;
border-radius: 5px;
margin: 180px auto;
width: 350px;
padding: 35px 35px 15px;
background: #fff;
border: 1px solid #eaeaea;
box-shadow: 0 0 25px #cac6c6;
}
label.el-checkbox.rememberme {
margin: 0px 0px 15px;
text-align: left;
}
</style>
增加路由
訪問(wèn) login 要跳轉(zhuǎn)到登錄頁(yè)面
修改 router/index.js 為
import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'
import Home from '../views/Home.vue'
Vue.use(VueRouter)
const routes = [
{
path: '/',
name: 'home',
component: Home
},
{
path: '/login',
name: 'login',
component: Login
}
]
const router = new VueRouter({
mode: 'history',
base: process.env.BASE_URL,
routes
})
export default router
訪問(wèn) http://127.0.0.1:8080/login 出現(xiàn)登錄頁(yè)面:
TIM圖片20200115155516.png
文章改編自大神博客:https://www.cnblogs.com/wbjxxzx/p/9942648.html理郑,僅作為學(xué)習(xí)實(shí)踐拴竹。