vue-cli3 + elementui 搭建后臺(tái)管理界面(四)-使用font-awesome

使用font-awesome

npm install --save font-awesome

修改 src/main.js 增加

import 'font-awesome/scss/font-awesome.scss'

1 login頁面增加圖標(biāo)

<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="用戶名"
            >
            <template slot="prepend">
              <span class="fa fa-user fa-lg" style="width: 13px"></span>
            </template>
          </el-input>
        </el-form-item>
          <el-form-item prop="password">
            <el-input :type="pwdType"
                      v-model="myForm.password"
                      auto-complete="off"
                      placeholder="密碼"
              >
              <template slot="prepend">
                <span class="fa fa-lock fa-lg"
                      style="width: 13px"></span>
              </template>
              <template slot="suffix">
                <span class="password-eye"
                      @click="showPassword"
                      :class="eyeType"
                      style="width: 13px"></span>
              </template>
            </el-input>
          </el-form-item>
        <el-checkbox v-model="rememberme" 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>

修改樣式

<style lang="scss" scoped>
.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;
  .password-eye {
    position: absolute;
    right: 20px;
    top: 11px;
    cursor: pointer;
  }
}
label.el-checkbox.rememberme {
  margin: 0px 0px 15px;
  text-align: left;
}
</style>

效果如下


image

給"眼睛"增加click事件

<script>
export default {
  data(){
    return {
      logining: false,
      myForm: {
        username: '',
        password: '',
      },
      myRule: {
        username: [{ required: true, message: 'please enter your account', trigger: 'blur' }],
        password: [{ required: true, message: 'enter your password', trigger: 'blur' }]
      },
      rememberme: false,
      pwdType: 'password',
      eyeType: 'fa fa-eye-slash fa-lg',
    }
  },
  methods: {
    showPassword() {
      if (this.pwdType === 'password') {
        this.pwdType = ''
        this.eyeType = 'fa fa-eye fa-lg'
      } else {
        this.pwdType = 'password'
        this.eyeType = 'fa fa-eye-slash fa-lg'
      }
    },
    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>

2 簡單實(shí)現(xiàn)記住密碼

成功登陸后把用戶名和密碼存入cookie,再次進(jìn)入頁面時(shí)讀取cookie

<script>
export default {
  data(){
    return {
      logining: false,
      myForm: {
        username: '',
        password: '',
      },
      myRule: {
        username: [{ required: true, message: '請(qǐng)輸入用戶名', trigger: 'blur' }],
        password: [{ required: true, message: '請(qǐng)輸入密碼', trigger: 'blur' }]
      },
      rememberme: false,
      pwdType: 'password',
      eyeType: 'fa fa-eye-slash fa-lg',
      redirect: undefined,
    }
  },
  methods: {
    showPassword() {
      if (this.pwdType === 'password') {
        this.pwdType = ''
        this.eyeType = 'fa fa-eye fa-lg'
      } else {
        this.pwdType = 'password'
        this.eyeType = 'fa fa-eye-slash fa-lg'
      }
    },
    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);
              if(this.rememberme){
                //console.log(this.myForm.password)
                this.setCookie(this.myForm.username, this.myForm.password, 7)
              }else{
                this.clearCookie()
              }
              this.$router.push({path: '/'});
          }else{
            this.logining = false;
            this.$alert('賬號(hào)或密碼錯(cuò)誤', '溫馨提示', {
                confirmButtonText: '確定'
            })
          }
        }else{
          console.log('error submit!');
          return false;
        }
      })
    },
    setCookie(name, pass, days){
      let expire = new Date()
      expire.setDate(expire.getDate() + days)
      document.cookie = `C-username=${name};expires=${expire}`
      document.cookie = `C-password=${pass};expires=${expire}`
    },
    clearCookie(){
      this.setCookie('', '', -1)
    },
    getCookie(){
      if(document.cookie.length){
        let arr = document.cookie.split('; ')
        for(let i=0; i<arr.length; i++){
          let arr2 = arr[i].split('=')
          if(arr2[0] === 'C-username'){
            this.myForm.username = arr2[1]
          }else if(arr2[0] === 'C-password'){
            this.myForm.password = arr2[1]
            this.rememberme = true
          }
        }
      }
    },
  },
  mounted(){
    this.getCookie()
  }
};
</script>

注意修改之前寫在main.js里面的路由導(dǎo)航守衛(wèi),先將其隱藏,后期再處理

import Vue from 'vue'
import App from './App'
import store from './store'
import router from './router'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import './assets/css/index.scss'
import 'font-awesome/scss/font-awesome.scss'

Vue.use(ElementUI)
Vue.config.productionTip = false

// router.beforeEach((to, from, next) => {
//   if (to.path === '/login') {
//     sessionStorage.removeItem('user')
//   }
//   var user = sessionStorage.getItem('user')
//   if (!user && to.path !== '/login') {
//     next({
//       path: '/login'
//     })
//   } else {
//     next()
//   }
// })

new Vue({
  el: '#app',
  store,
  router,
  render: h => h(App)
})

文章改編自大神博客:https://www.cnblogs.com/wbjxxzx/p/9975933.html,僅作為學(xué)習(xí)實(shí)踐。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容