docs(PullRefresh): use composition api

This commit is contained in:
chenjiahan 2020-12-10 21:00:30 +08:00
parent e785f622bd
commit b3dfb4997f
3 changed files with 66 additions and 55 deletions

View File

@ -17,29 +17,33 @@ app.use(PullRefresh);
The `refresh` event will be Emitted when pull refresh, you should set `v-model` to `false` to reset loading status after process refresh event. The `refresh` event will be Emitted when pull refresh, you should set `v-model` to `false` to reset loading status after process refresh event.
```html ```html
<van-pull-refresh v-model="isLoading" @refresh="onRefresh"> <van-pull-refresh v-model="state.loading" @refresh="onRefresh">
<p>Refresh Count: {{ count }}</p> <p>Refresh Count: {{ state.count }}</p>
</van-pull-refresh> </van-pull-refresh>
``` ```
```js ```js
import { reactive } from 'vue';
import { Toast } from 'vant'; import { Toast } from 'vant';
export default { export default {
data() { setup() {
return { const state = reactive({
count: 0, count: 0,
isLoading: false, loading: false,
}; });
}, const onRefresh = () => {
methods: {
onRefresh() {
setTimeout(() => { setTimeout(() => {
Toast('Refresh Success'); Toast('Refresh Success');
this.isLoading = false; state.loading = false;
this.count++; state.count++;
}, 1000); }, 1000);
}, };
return {
state,
onRefresh,
};
}, },
}; };
``` ```

View File

@ -21,29 +21,33 @@ app.use(PullRefresh);
下拉刷新时会触发 `refresh` 事件,在事件的回调函数中可以进行同步或异步操作,操作完成后将 `v-model` 设置为 `false`,表示加载完成。 下拉刷新时会触发 `refresh` 事件,在事件的回调函数中可以进行同步或异步操作,操作完成后将 `v-model` 设置为 `false`,表示加载完成。
```html ```html
<van-pull-refresh v-model="isLoading" @refresh="onRefresh"> <van-pull-refresh v-model="state.loading" @refresh="onRefresh">
<p>刷新次数: {{ count }}</p> <p>刷新次数: {{ state.count }}</p>
</van-pull-refresh> </van-pull-refresh>
``` ```
```js ```js
import { reactive } from 'vue';
import { Toast } from 'vant'; import { Toast } from 'vant';
export default { export default {
data() { setup() {
return { const state = reactive({
count: 0, count: 0,
isLoading: false, loading: false,
}; });
}, const onRefresh = () => {
methods: {
onRefresh() {
setTimeout(() => { setTimeout(() => {
Toast('刷新成功'); Toast('刷新成功');
this.isLoading = false; state.loading = false;
this.count++; state.count++;
}, 1000); }, 1000);
}, };
return {
state,
onRefresh,
};
}, },
}; };
``` ```

View File

@ -1,14 +1,14 @@
<template> <template>
<van-tabs> <van-tabs>
<van-tab :title="t('basicUsage')"> <van-tab :title="t('basicUsage')">
<van-pull-refresh v-model="isLoading" @refresh="onRefresh(true)"> <van-pull-refresh v-model="loading" @refresh="onRefresh(true)">
<p>{{ tips }}</p> <p>{{ tips }}</p>
</van-pull-refresh> </van-pull-refresh>
</van-tab> </van-tab>
<van-tab :title="t('successTip')"> <van-tab :title="t('successTip')">
<van-pull-refresh <van-pull-refresh
v-model="isLoading" v-model="loading"
:success-text="t('success')" :success-text="t('success')"
@refresh="onRefresh(false)" @refresh="onRefresh(false)"
> >
@ -18,7 +18,7 @@
<van-tab :title="t('customTips')"> <van-tab :title="t('customTips')">
<van-pull-refresh <van-pull-refresh
v-model="isLoading" v-model="loading"
head-height="80" head-height="80"
@refresh="onRefresh(true)" @refresh="onRefresh(true)"
> >
@ -42,6 +42,10 @@
</template> </template>
<script> <script>
import { computed, onMounted, reactive, toRefs } from 'vue';
import { useTranslate } from '../../composables/use-translate';
import Toast from '../../toast';
export default { export default {
i18n: { i18n: {
'zh-CN': { 'zh-CN': {
@ -60,47 +64,46 @@ export default {
}, },
}, },
data() { setup() {
return { const t = useTranslate();
const state = reactive({
count: 0, count: 0,
isLoading: false, loading: false,
}; });
},
computed: { const tips = computed(() => {
tips() { if (state.count) {
if (this.count) { return `${t('text')}: ${state.count}`;
return `${this.t('text')}: ${this.count}`;
} }
return t('try');
});
return this.t('try'); const onRefresh = (showToast) => {
}, setTimeout(() => {
}, if (showToast) {
Toast(t('success'));
}
state.loading = false;
state.count++;
}, 1000);
};
mounted() { const preloadImage = () => {
this.preloadImage();
},
methods: {
preloadImage() {
// preload doge image // preload doge image
const doge = new Image(); const doge = new Image();
const dogeFire = new Image(); const dogeFire = new Image();
doge.src = 'https://b.yzcdn.cn/vant/doge.png'; doge.src = 'https://b.yzcdn.cn/vant/doge.png';
dogeFire.src = 'https://b.yzcdn.cn/vant/doge-fire.jpg'; dogeFire.src = 'https://b.yzcdn.cn/vant/doge-fire.jpg';
}, };
onRefresh(showToast) { onMounted(preloadImage);
setTimeout(() => {
if (showToast) {
this.$toast(this.t('success'));
}
this.isLoading = false; return {
this.count++; ...toRefs(state),
}, 1000); tips,
}, onRefresh,
};
}, },
}; };
</script> </script>