欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue3封裝登錄功能的兩種實現(xiàn)

 更新時間:2022年03月17日 10:13:59   作者:柒個M  
本文主要介紹了Vue3封裝登錄功能的兩種實現(xiàn),文中根據(jù)實例編碼詳細介紹的十分詳盡,具有一定的參考價值,感興趣的小伙伴們可以參考一下

方法一: 使用用戶名和密碼進行登錄

封裝代碼:

<template>
  <el-form
      ref="ruleFormRef"
      status-icon
      :model="ruleForms"
      :rules="rules"
      label-width="120px"
      class="demo-ruleForm"
  >
    <el-form-item label="用戶名:" prop="username">
      <el-input v-model="ruleForms.username" autocomplete="off">
        <template #prefix>
          <el-icon class="el-input__icon">
            <user/>
          </el-icon>
        </template>
      </el-input>
    </el-form-item>
    <el-form-item label="密碼:" prop="password">
      <el-input type="password" v-model="ruleForms.password" placeholder="Type something">
        <template #prefix>
          <el-icon class="el-input__icon">
            <search/>
          </el-icon>
        </template>
      </el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="handleLogin">Submit</el-button>
    </el-form-item>
  </el-form>
</template>
 
<script lang="ts" setup>
import {User, Search} from '@element-plus/icons-vue'
import {defineProps, reactive, ref, defineEmits, onMounted, watch} from 'vue'
 
let ruleForms = reactive({username: '', password: ''})
const ruleFormRef = ref(null)
const emits = defineEmits(['onSubmit', 'onError'])
 
const props = defineProps({
  ruleForm: {
    type: Object,
    required: true
  },
  rules: {
    type: Object,
    required: true
  }
})
 
onMounted(() => {
  ruleForms = props.ruleForm
})
 
watch(
    () => props.ruleForm,
    val => {
      ruleForms = val
    },
    {immediate: true}
)
 
// 登錄功能
const handleLogin = () => {
  ruleFormRef.value.validate(valid => {
    if (valid) {
      emits('onSubmit')
    } else {
      emits('onError')
    }
  })
}
</script>
 
<style scoped>
 
</style>

使用:

<template>
  <div class="login">
 
    <div class="account-panel">
      <account-login
          :ruleForm="ruleForm"
          :rules="rules"
          @onSubmit="onSubmit"
          @onError="onError"
      />
    </div>
  </div>
</template>
 
<script setup>
import {reactive} from 'vue'
import {ElMessage} from 'element-plus'
 
 
const ruleForm = reactive({
  username: '',
  password: ''
})
const rules = reactive({
  username: [
    {
      required: true,
      trigger: 'blur',
      message: '用戶名長度在2-6之間'
    }
  ],
  password: [
    {
      required: true,
      trigger: 'blur',
      validator: '密碼不能為空'
    }
  ]
})
const onSubmit = () => {
  alert('發(fā)送 http請求:')
  ElMessage({
    type: 'success',
    message: '保存成功'
  })
}
const onError = () => {
  ElMessage({
    type: 'warning',
    message: '校驗失敗'
  })
}
 
</script>
 
<style scoped lang="scss">
.login {
  margin-top: 50px;
  margin-left: 20px;
}
 
.account-panel {
  width: 350px;
  height: 350px;
}
</style>

方法二: 使用手機驗證碼登錄

封裝代碼:

<template>
  <div>
    <el-form :model="rulesForm" :rules="rules" ref="rulesRef">
      <el-form-item label="手機號:" prop="phone">
        <el-input v-model="rulesForm.phone" placeholder="請輸入手機號">
        </el-input>
      </el-form-item>
      <el-form-item label="驗證碼:" prop="countDown">
        <el-row :gutter="24">
          <el-col :span="18">
            <el-input v-model="rulesForm.countDown" placeholder="驗證碼"></el-input>
          </el-col>
          <el-col :span="6">
            <el-button @click="sendCode" :disabled="disabled">{{ btnText }}</el-button>
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleLogin">Submit</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
 
<script lang="ts" setup>
import {defineProps, ref, reactive, watch, defineEmits} from 'vue'
import {ElMessage} from "element-plus";
 
const checkPhone = (rule, value, callback) => {
  if (!value) {
    return callback(new Error('手機號不能為空!'))
  } else {
    let reg = /^1[3|4|5|7|8][0-9]\d{8}$/
    if (reg.test(value)) {
      callback()
    } else {
      return callback(new Error('請輸入正確的手機號!'))
    }
  }
}
 
const rulesForm = ref({})
const disabled = ref(false)
const btnText = ref('發(fā)送驗證碼')
 
const props = defineProps({
  ruleForm: {
    type: Object,
    required: true
  },
  countDown: {
    type: Number,
    required: true
  }
})
const emits = defineEmits(['sendCode'])
const rules = reactive({
  phone: [{required: true, trigger: 'change', validator: checkPhone,}],
  countDown: [{required: true, message: '驗證碼不能為空'}]
})
const rulesRef = ref(null)
const time = ref(0) // 設置倒計時
 
// 發(fā)送驗證碼
const sendCode = () => {
  // 手機號必須輸入正確,如果不正確,就提示
  rulesRef.value.validateField('phone', errMessage => {
    if (!errMessage) {
      ElMessage({
        type: 'error',
        message: '請輸入正確的手機號'
      })
    } else {
      // 1、時間開始倒數(shù)
      // 2、按鈕進入禁用狀態(tài)
      // 3、如果倒計時結束,按鈕恢復可用狀態(tài),按鈕文字變成重新發(fā)送
      // 4、倒計時的過程中,按鈕文字為多少秒后重新發(fā)送
      time.value = props.countDown
      let timer = setInterval(() => {
        time.value--
        btnText.value = `${time.value}s后重新發(fā)送`
        disabled.value = true
        if (time.value === 0) {
          disabled.value = false
          btnText.value = '重新發(fā)送'
          time.value = props.countDown
          clearInterval(timer)
        }
      }, 1000)
      emits('sendCode')
    }
  })
}
 
// 登錄功能
const handleLogin = () => {
  rulesRef.value.validate(valid => {
    if (valid) {
      emits('onSubmit')
    } else {
      emits('onError')
    }
  })
}
watch(
    () => props.ruleForm,
    val => {
      rulesForm.value = val
    },
    {immediate: true}
)
 
</script>
 
<style scoped>
 
</style>

使用:

<template>
  <div class="login-panel">
    <div class="phone-login">
      <phone-login
          :ruleForm="ruleForm"
          :countDown="countDown"
          @sendCode="sendCode"
          @onSubmit="onSubmit"
          @onError="onError"
      >
      </phone-login>
    </div>
 
  </div>
</template>
 
<script setup>
import {reactive, ref} from "vue";
import {ElMessage} from "element-plus";
 
const ruleForm = reactive({
  phone: '',
  countDown: ''
})
const countDown = ref(30)
 
const sendCode = () => {
  // 發(fā)送驗證碼接口
  ElMessage({
    type: 'success',
    message: '發(fā)送驗證碼成功'
  })
}
 
const onSubmit = () => {
  alert('發(fā)送 http請求:')
  ElMessage({
    type: 'success',
    message: '保存成功'
  })
}
 
const onError = () => {
  ElMessage({
    type: 'warning',
    message: '校驗失敗'
  })
}
</script>
 
<style scoped lang="scss">
.login-panel {
  margin-left: 20px;
  margin-top: 20px;
}
 
.phone-login {
  width: 350px;
  height: 350px;
}
</style>

到此這篇關于Vue3封裝登錄功能的兩種實現(xiàn)的文章就介紹到這了,更多相關Vue3 登錄內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • vue router路由嵌套不顯示問題的解決方法

    vue router路由嵌套不顯示問題的解決方法

    這篇文章主要為大家詳細介紹了vue router路由嵌套不顯示的問題,具有一定的參考價值,感興趣的小伙伴們可以參考一下vue-router 路由嵌套不顯示問題
    2017-06-06
  • 初探Vue3.0 中的一大亮點Proxy的使用

    初探Vue3.0 中的一大亮點Proxy的使用

    這篇文章主要介紹了初探Vue3.0 中的一大亮點Proxy的使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-12-12
  • 基于Vue-Cli 打包自動生成/抽離相關配置文件的實現(xiàn)方法

    基于Vue-Cli 打包自動生成/抽離相關配置文件的實現(xiàn)方法

    基于Vue-cli 項目產品部署,涉及到的交互的地址等配置信息,每次都要重新打包才能生效,極大的降低了效率。這篇文章主要介紹了基于Vue-Cli 打包自動生成/抽離相關配置文件 ,需要的朋友可以參考下
    2018-12-12
  • 徹底搞懂Transition內置組件

    徹底搞懂Transition內置組件

    這篇文章主要為大家介紹了Transition內置組件使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-07-07
  • vue配置生產環(huán)境.env.production與測試環(huán)境.env.development

    vue配置生產環(huán)境.env.production與測試環(huán)境.env.development

    這篇文章主要介紹了vue配置生產環(huán)境.env.production與測試環(huán)境.env.development方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • vue使用H5的audio標簽問題

    vue使用H5的audio標簽問題

    這篇文章主要介紹了vue使用H5的audio標簽問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • 利用angular、react和vue實現(xiàn)相同的面試題組件

    利用angular、react和vue實現(xiàn)相同的面試題組件

    eact 和angular,vue 這三個框架最近都比較火,下面這篇文章主要給大家介紹了關于利用angular、react和vue實現(xiàn)相同的面試題組件的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下。
    2018-02-02
  • axios全局請求參數(shù)設置,請求及返回攔截器的方法

    axios全局請求參數(shù)設置,請求及返回攔截器的方法

    下面小編就為大家分享一篇axios全局請求參數(shù)設置,請求及返回攔截器的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-03-03
  • vue實例中data使用return包裹的方法

    vue實例中data使用return包裹的方法

    今天小編就為大家分享一篇vue實例中data使用return包裹的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-08-08
  • 詳解如何在vue中封裝axios請求并集中管理

    詳解如何在vue中封裝axios請求并集中管理

    這篇文章主要為大家詳細介紹了如何在vue中封裝axios請求并集中管理,w文中的示例代碼講解詳細,具有一定的參考價值,有需要的小伙伴可以了解下
    2023-10-10

最新評論