1.0.0 • Published 2 years ago

mxphenix-hooks_taro_vue v1.0.0

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

目录

useToggle

用于两个开关状态之间切换的 Hook

代码演示

<template>
  <view>状态值:{{ state ? '开' : '关' }}</view>
  <button @click='off'>关</button>
  <button @click='on'>开</button>
  <button @click='reverse'>切换</button>
</template>

<script setup lang="ts">
import { useToggle } from '@hooks/index';
const { state, on, off, reverse } = useToggle(false);
</script>

API

const { state, on, off, reverse } = useToggle(defaultValue?: boolean);

Params

参数说明类型默认
defaultValue可选项,传入默认的状态值booleanfalse

Result

参数说明类型
state状态值Ref\<boolean>
on开启() => void
off关闭() => void
reverse相反切换() => void

useVisible

用于显隐两种状态之间切换的Hook

代码演示

<template>
  <view v-if="visible">This is a good hook.</view>
</template>

<script setup lang="ts">
import { useVisible } from '@hooks/index';
const { visible, show, hide, reverse } = useVisible(false);
</script>

API

const { visible, show, hide, reverse } = useVisible(defaultValue?: boolean);

Params

参数说明类型默认
defaultValue可选项,传入默认的状态值booleanfalse

Result

参数说明类型
visible状态值Ref\<boolean>
show显示() => void
hide隐藏() => void
reverse相反切换() => void

useLoading

用于加载状态之间切换的Hook

代码演示

<template>
  <view v-if="loading">loading...</view>
  <button @click='hideLoading'>取消</button>
  <button @click='showLoading'>加载</button>
</template>

<script setup lang="ts">
import { useLoading } from '@hooks/index';
const { loading, showLoading, hideLoading } = useLoading(false);
</script>

API

const { loading, showLoading, hideLoading } = useLoading(defaultValue?: boolean);

Params

参数说明类型默认
defaultValue可选项,传入默认的状态值booleanfalse

Result

参数说明类型
loading状态值Ref\<boolean>
showLoading加载() => void
hideLoading隐藏() => void

useClassName

用于管理组件私有化样式类名

<template>
  <SafeArea position="both">
    <view :class="className('__background')">
      <view :class="className('__background__header')">
        <text :class="className('__background__header__title')"
          >欢迎登录柏溪福利小程序</text
        >
        <text :class="className('__background__header__desc')"
          >完善信息,我们将会为您提供专属服务</text
        >
        <text :class="className(['__background__header__tips', '__background__header__tips--active'])"
          >完善信息,我们将会为您提供专属服务</text
        >
      </view>
    </view>
  </SafeArea>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { useClassName } from '@hooks/index';
import { SafeArea } from '@components/index';

const { className } = useClassName('_login');
</script>

<style lang="scss">
@import './index.scss';
</style>

API

const { className } = useClassName(prefixCls: string);

Params

参数说明类型默认
prefixCls必选项,传入前缀类名string-

Result

参数说明类型
className设置样式类名(className: string | string[]) => string

useAsync

用于处理异步任务的Hook

代码演示

<template>
  <view v-if="loading">loading...</view>
  <view v-if="data">
    <view v-for="item in data">{{ item.title }}</view>
  </view>
  <view v-if="error">{{ error }}</view>
</template>

<script setup lang="ts">
import { useAsync } from '@hooks/index';

const delay = (timer = 1000) => {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(Math.random());
    }, timer);
  });
};

const { data, loading, error, run } = useAsync(
  async () => {
    await delay(100);
    await delay(200);
  },
  {
    immediate: true,
    cache: false,
    onBefore(params) {
      console.log('params', params);
    },
    onSuccess(result) {
      console.log('result', result);
      console.log('params', params);
    },
    onError(error) {
      console.log('error', error);
    },
    onFinally(params, result, error) {
      console.log('result', result);
      console.log('params', params);
      console.log('error', error);
    },
  }
);
</script>

API

const { data, loading, error, run } = useAsync<T = any>(service: AsyncService<T>, option: AsyncOption<T>);

Params

type Params = Record<string, any>;

AsyncService

type AsyncService<T = any> = (params?: Params) => Promise<T>;

AsyncOption

type AsyncOption<T = any> = {
  immediate?: boolean;
  cache?: boolean;
  onBefore?: (params?: Params) => void;
  onSuccess?: (result: T, params?: Params) => void;
  onError?: (error: any) => void;
  onFinally?: (
    params: Params | undefined,
    result: T | undefined,
    error: any
  ) => void;
};

Result

参数说明类型
data请求成功返回的数据Ref\<T | undefined>
loading加载状态Re\<boolean>
error请求错误返回的数据Ref\<any>
run手动触发 service 执行,参数会传递给 service(params?: Params) => Promise\<T>

Params

参数说明类型默认
service必选项,传入serviceAsyncService\<T = any>-
options可选项,传入的配置选项AsyncOption\<T = any>-

Options

参数说明类型默认
immediate可选项,传入默认的立即执行booleanfalse
cache可选项,是否缓存响应数据booleantrue
onBefore可选项,传入请求前的操作(params?: Params) => void-
onSuccess可选项,传入请求成功的操作(onSuccess: T, params?: Params) => void-
onError可选项,传入请求错误的操作(error: any) => void-
onFinally可选项,传入请求完成的操作(params: Params | undefined, result: T | undefined, error: any) => void-

useRequest

用于处理服务请求的Hook

代码演示

<template>
  <view v-if="loading">loading...</view>
  <view v-if="data">
    <view v-for="item in data">{{ item.title }}</view>
  </view>
  <view v-if="error">{{ error }}</view>
</template>

<script setup lang="ts">
import { useRequest } from '@hooks/index';
import { GetUsers } from '@services/index';
const { data, loading, error } = useRequest<API.UserItem[]>(GetUsers, {
  immediate: true;
  cache: false,
  defaultParams: {
  	current: 1,
    size: 24
  }
});
</script>

API

const { data, params, loading, error, run, refresh } = useRequest<T = any>(service: AsyncService<T>, options: RequestOption<T>);

RequestOption

type RequestOption<T = any> = AsyncOption<T> & {
  defaultParams?: Params;
};

Result

参数说明类型
...同上 useAsync result 参数一致
params请求参数,可通过defaultParams默认设置Ref\<Params | undefined>
refresh刷新请求,不会覆盖参数() => void

Params

参数说明类型默认
service必选项,传入serviceAsyncService\<T = any>-
options可选项,传入的配置选项RequestOption\<T = any>-

Options

参数说明类型默认
...同上 AsyncOption 参数一致AsyncOption\<T = any>-
defaultParams可选项,传入默认的请求参数Params-

useLoadPage

用于处理分页请求的Hook

代码演示

<template>
  <view v-if="loading">loading...</view>
  <view v-if="data">
    <view v-for="item in data">{{ item.title }}</view>
  </view>
  <view v-if="error">{{ error }}</view>
  <button @click="prev">上一页</button>
  <button @click="next">下一页</button>
  <button @click="prev({ run: true })">上一页(请求)</button>
  <button @click="next({ run: true })">下一页(请求)</button>
</template>

<script setup lang="ts">
import { useLoadPage } from '@hooks/index';
import { GetUsers } from '@services/index';
const { data, loading, error, params, prev, next, paging } = useLoadPage<API.UserItem[]>(GetUsers, {
  immediate: true;
  defaultParams: {
  	current: 1,
    size: 24
  },
  defaultPaging: {
    pageNum: 1,
    size: 16,
  },
  pagingOption: {
    currentField: 'pageNum',
    sizeField: 'size',
  }
});
</script>

API

const { data, params, paging, loading, error, run, next, prev, refresh } = useLoadPage<T = any>(service: AsyncService<T>, options: LoadPageOption<T>);

LoadPageOption

type LoadPageOption<T = any> = RequestOption<T> & {
  defaultPaging?: Params;
  pagingOptions?: {
    currentField?: string;
    sizeField?: string;
  }
};

PageOption

type PageOption= { run?: boolean };

Result

参数说明类型
...同上 useRequest result 参数一致
paging分页参数Ref\<Params | undefined>
next下一页,传入{ run: true }分页请求,不传参仅分页(pageOpton?: PageOption) => void
prev上一页,传入{ run: true }分页请求,不传参仅分页(pageOpton?: PageOption) => void

Params

参数说明类型默认
service必选项,传入serviceAsyncService\<T = any>-
options可选项,传入的配置选项RequestOption\<T = any>-

Options

参数说明类型默认
...同上 RequestOption 参数一致RequestOption\<T = any>-
defaultPaging可选项,传入默认的分页参数Params-
pagingOptions可选项,分页配置选项PagingOptions-

Paging

参数说明类型默认
current可选项,分页当前索引,currentField可配置自定义字段any1
size可选项,分页大小,sizeField可配置自定义字段any16

pagingOptions

参数说明类型默认
currentField可选项,分页当前索引字段stringcurrent
sizeField可选项,分页大小字段stringsize

useLoadRefresh

用于处理请求刷新的hook

代码演示

<template>
  <view v-if="loading">loading...</view>
  <view v-if="data">{{ data }}</view>
  <buttonn @click="run">刷新</buttonn>
  <buttonn @click="run({ pullDownRefresh: true })">触发下拉刷新</buttonn>
</template>

<script setup lang="ts">
import { useRequest, useLoadRefresh } from '@hooks/index';
import { GetUsers, GetProfile } from '@services/index';

const {
  data: users,
  loading: userLoading,
  run: userAsync
} = useRequest<API.UserItem[]>(GetUsers, {
  immediate: false
});

const {
  data: profile,
  loading: profileLoading,
  run: profileAsync
} = useRequest<API.UserProfile>(GetProfile, {
  immediate: false
});

const { data, loading, run } = useLoadRefresh(
  async () => {
    await userAsync();
    await profileAsync();
  },
  { immediate: true }
});
</script>

API

const { data, loading, error, run } = useLoadRefresh<T = any>(service: AsyncService<T>, option: AsyncOption<T>);

Result

参数说明类型
...同上 useAsync result 参数一致
run手动触发 service 执行,参数会传递给 service(runOption?: RunOption) => Promise\<T>

RunOption

参数说明类型默认
pullDownRefresh下拉刷新booleanfalse
1.0.0

2 years ago