0.1.1 • Published 9 months ago

qrany-vue-promise v0.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

使用方式

Await

基本使用

<script setup lang="ts">
  import axios from "axios";
  import { Await } from "qrany-vue-promise";

  const promise = axios.get("");
</script>

<template>
  <Await :promise="promise">
    <template #loading>loading...</template>
    <template #error="{ error }">{{ error }}</template>
    <template #placeholder>无返回时的占位</template>
    <template #default="{ value }"></template>
  </Await>

  <Await :promise="promise">
    <template #error="{ error }">{{ error }}</template>
    <template #placeholder>Loading...</template>
    <template #default="{ value }"></template>
  </Await>

  <Await :promise="promise">
    <template #placeholder>无返回时的占位</template>
    <template #default="{ value }"></template>
  </Await>
</template>

页面更新

<script setup lang="ts">
  import axios from "axios";
  import { Await } from "qrany-vue-promise";
  import { shallowRef } from "vue";

  const promise = shallowRef<Promise<unknown>>();
  const onSave = () => (promise.value = axios.post(""));
</script>

<template>
  <Await :promise="promise">
    <template #loading>
      <button disabled>提交中</button>
    </template>
    <template #default>
      <button @click="onSave">提交</button>
    </template>
  </Await>
</template>

usePromise

基本使用

<script setup lang="ts">
  import axios from "axios";
  import { usePromise } from "qrany-vue-promise";

  const getData = () => axios.get("");
  const [data] = usePromise(getData);
</script>

<template>
  <template v-if="data.loading">loading...</template>
  <template v-else-if="data.error">{{ data.error }}</template>
  <template v-else-if="data.value">{{ data.value }}</template>
  <template v-else>无返回</template>
</template>

页面更新

<script setup lang="ts">
  import axios from "axios";
  import { usePromise } from "qrany-vue-promise";
  import { shallowReactive } from "vue";
  import { useRoute } from "vue-router";

  const route = useRoute();
  const form = shallowReactive<{ type?: string }>({});
  const getData = () => axios.get("", { params: { id: route.query.id, type: form.type } });

  const [data] = usePromise(getData);
</script>

<template>
  <template v-if="data.loading">loading...</template>
  <template v-else-if="data.error">{{ data.error }}</template>
  <template v-else-if="data.value">{{ data.value }}</template>
  <template v-else>无返回</template>
</template>

表单提交

<script setup lang="ts">
  import axios from "axios";
  import { usePromise } from "qrany-vue-promise";
  import { shallowRef } from "vue";
  import { useRoute } from "vue-router";

  // 方式1
  const [save, dispatch] = usePromise();
  const onSave = () => dispatch(axios.post(""));

  // 方式2
  const promise = shallowRef<Promise<unknown>>();
  const [save] = usePromise(promise);
  const onSave = () => (promise.value = axios.post(""));
</script>

<template>
  <button :disabled="save.loading" @click="onSave">
    <template v-if="save.loading">提交中...</template>
    <template v-else>提交</template>
  </button>
</template>

API

Await

属性

属性名类型默认值说明
promisePromise<T> \| void-请求的 Promise

插槽

插槽名说明参数
default请求成功后的内容{ value: T }
placeholder无返回时的占位-
loading请求中的内容-
error请求失败的内容{ error: unknown }

usePromise

参数

属性名类型默认值说明
promiseWatchSource<Promise<T> \| void>-请求的 Promise
defaultValueT-默认值

返回值

Data

属性名类型默认值说明
loadingbooleanfalse请求中
errorunknown-请求失败
valueT-请求成功

,Dispatch

参数
属性名类型默认值说明
promisePromise<T>-请求的 Promise
返回值
类型默认值说明
Promise<void>-等待异步
0.1.1

9 months ago

0.1.0

9 months ago

0.0.0

9 months ago