|
| 1 | +## 目标 |
| 2 | +分析业务逻辑与页面渲染分离的好处 |
| 3 | +完成账号登录业务逻辑 |
| 4 | + |
| 5 | +## 分析 |
| 6 | +在过去vue2的开发中,我们通常会把所有的业务逻辑和UI界面都在一个vue文件中去完成,导致了整个页面中代码逻辑过多,后期我们想复用这一部分的代码,可能需要花费大量的时间去提取,或者直接复制粘贴,导致大量的代码冗余的问题,所以我们为了避免这种问题,我们可以考虑在vue3中使用组合式API的方式,来抽离业务代码,使得业务代码和UI界面分离更容易维护、扩展和复用等。 |
| 7 | +大家想了解更多关于组合式API的问题可以去[组合式 API 常见问答](https://cn.vuejs.org/guide/extras/composition-api-faq.html),里面详细的介绍了传统API和组合式API的区别和优劣势。 |
| 8 | +## 开发 |
| 9 | +我们在pages/login文件夹下创建一个composables的组合式API的文件夹,用于存放我们login文件夹下所有的与业务相关的组合式API。 |
| 10 | +然后我们创建一个account-login.ts的文件用于编写我们的登录部分业务逻辑。如下: |
| 11 | + |
| 12 | +```typescript |
| 13 | +import type { FormInst } from 'naive-ui' |
| 14 | +import type { UserAccountLoginParams } from '~/api/user' |
| 15 | + |
| 16 | +export const useAccountLogin = () => { |
| 17 | + const formRef = ref<FormInst>() |
| 18 | + const loading = ref(false) |
| 19 | + |
| 20 | + const model = reactive<UserAccountLoginParams>({ |
| 21 | + username: null, |
| 22 | + password: null, |
| 23 | + }) |
| 24 | + |
| 25 | + const login = async () => { |
| 26 | + |
| 27 | + } |
| 28 | + |
| 29 | + return { |
| 30 | + formRef, |
| 31 | + loading, |
| 32 | + model, |
| 33 | + login, |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +``` |
| 38 | +我们写完这一部分后,会发现参数报错。是因为默认情况下我们的`UserAccountLoginParams`不支持null类型所以我们需要给我们的类型添加一个null类型。如下: |
| 39 | +在utils目录下创建一个types.ts的文件用于存放我们自己定义的通用类型。 |
| 40 | +```typescript |
| 41 | +export type IncludeNull<T> = T | null |
| 42 | +``` |
| 43 | +调整api/user.ts中的类型: |
| 44 | +```typescript |
| 45 | +import type { IncludeNull } from '~/utils/types' |
| 46 | + |
| 47 | +export interface UserAccountLoginParams { |
| 48 | + username: IncludeNull<string> |
| 49 | + password: IncludeNull<string> |
| 50 | + captcha?: IncludeNull<string> |
| 51 | +} |
| 52 | + |
| 53 | +export interface UserMobileLoginParams { |
| 54 | + mobile: IncludeNull<string> |
| 55 | + code: IncludeNull<string> |
| 56 | + type: 'mobile' |
| 57 | +} |
| 58 | +``` |
| 59 | +调整完成后我们的报错也就随之消失了。 |
| 60 | +接下来我们来配置一下完善一下校验规则: |
| 61 | +```typescript |
| 62 | + const rules = reactive<FormRules>({ |
| 63 | + username: [ |
| 64 | + { |
| 65 | + required: true, |
| 66 | + renderMessage: () => t('login.username.required'), |
| 67 | + }, |
| 68 | + { |
| 69 | + min: 5, |
| 70 | + max: 20, |
| 71 | + renderMessage: () => t('login.username.length'), |
| 72 | + }, |
| 73 | + ], |
| 74 | + password: [ |
| 75 | + { |
| 76 | + required: true, |
| 77 | + renderMessage: () => t('login.password.required'), |
| 78 | + }, |
| 79 | + { |
| 80 | + min: 5, |
| 81 | + max: 20, |
| 82 | + renderMessage: () => t('login.password.length'), |
| 83 | + }, |
| 84 | + ], |
| 85 | + }) |
| 86 | + |
| 87 | +``` |
| 88 | +最后return出去。 |
| 89 | +接下来我们来完善一下我们的登录接口: |
| 90 | +```typescript |
| 91 | +// 1.加载loading状态 |
| 92 | +// 2.校验表单是否正确 |
| 93 | +// 3.请求接口 |
| 94 | +// 4.请求成功设置token |
| 95 | +// 5.跳转到首页 |
| 96 | + |
| 97 | +const login = async () => { |
| 98 | + loading.value = true |
| 99 | + try { |
| 100 | + await formRef.value?.validate() |
| 101 | + await userStore.login(model) |
| 102 | + loading.value = false |
| 103 | + const redirect = router.currentRoute.value?.params?.redirect as string |
| 104 | + await router.replace(redirect || '/') |
| 105 | + } |
| 106 | + catch (e) { |
| 107 | + loading.value = false |
| 108 | + } |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | + |
| 113 | +接下来我们在项目中使用一下: |
| 114 | +在pages/login/index.vue中使用: |
| 115 | +```vue |
| 116 | +<script lang="ts" setup> |
| 117 | +const { rules, formRef, login, loading, model } = useAccountLogin() |
| 118 | +</script> |
| 119 | +<template> |
| 120 | + <n-form ref="formRef" :model="model" :rules="rules" label-align="left" label-placement="left"> |
| 121 | + <n-form-item-row path="username"> |
| 122 | + <n-input v-model:value="model.username" :placeholder="$t('login.username.placeholder')"> |
| 123 | + <template #prefix> |
| 124 | + <n-icon :component="UserOutlined" /> |
| 125 | + </template> |
| 126 | + </n-input> |
| 127 | + </n-form-item-row> |
| 128 | + <n-form-item-row path="password"> |
| 129 | + <n-input v-model:value="model.password" type="password" show-password-on="click" :placeholder="$t('login.password.placeholder')"> |
| 130 | + <template #prefix> |
| 131 | + <n-icon :component="LockOutlined" /> |
| 132 | + </template> |
| 133 | + </n-input> |
| 134 | + </n-form-item-row> |
| 135 | + <n-form-item-row path="rememberMe"> |
| 136 | + <div class="w-100% flex items-center justify-between"> |
| 137 | + <n-checkbox v-model:value="model.rememberMe"> |
| 138 | + {{ $t('login.remember-me') }} |
| 139 | + </n-checkbox> |
| 140 | + <a class="cursor-pointer text-[var(--text-color-3)]"> |
| 141 | + {{ $t('login.forgot-password') }} |
| 142 | + </a> |
| 143 | + </div> |
| 144 | + </n-form-item-row> |
| 145 | + </n-form> |
| 146 | + <n-button type="primary" :loading="loading" block secondary strong @click="login"> |
| 147 | + {{ $t('login.login') }} |
| 148 | + </n-button> |
| 149 | +</template> |
| 150 | +
|
| 151 | +``` |
| 152 | +测试是否可用 |
0 commit comments