docs(Cascader): improve async loading demo (#11666)

This commit is contained in:
neverland 2023-03-12 21:23:38 +08:00 committed by GitHub
parent bb64d6eedb
commit 065caff468
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 6 deletions

View File

@ -114,6 +114,7 @@ export default {
```js
import { ref } from 'vue';
import { closeToast, showLoadingToast } from 'vant';
export default {
setup() {
@ -128,13 +129,19 @@ export default {
},
]);
const onChange = ({ value }) => {
if (value === options.value[0].value) {
if (
value === options.value[0].value &&
options.value[0].children.length === 0
) {
showLoadingToast('Loading...');
// mock data request
setTimeout(() => {
options.value[0].children = [
{ text: 'Hangzhou', value: '330100' },
{ text: 'Ningbo', value: '330200' },
];
}, 500);
closeToast();
}, 1000);
}
};
const onFinish = ({ selectedOptions }) => {

View File

@ -184,6 +184,7 @@ export default {
```js
import { ref } from 'vue';
import { closeToast, showLoadingToast } from 'vant';
export default {
setup() {
@ -198,13 +199,19 @@ export default {
},
]);
const onChange = ({ value }) => {
if (value === options.value[0].value) {
if (
value === options.value[0].value &&
options.value[0].children.length === 0
) {
// 模拟数据请求
showLoadingToast('加载中...');
setTimeout(() => {
options.value[0].children = [
{ text: '杭州市', value: '330100' },
{ text: '宁波市', value: '330200' },
];
}, 500);
closeToast();
}, 1000);
}
};
const onFinish = ({ selectedOptions }) => {

View File

@ -9,6 +9,7 @@ import zhCNOptions from './area-zh-CN';
import enUSOptions from './area-en-US';
import { useCurrentLang } from '../../locale';
import { useCascaderAreaData } from '@vant/area-data';
import { closeToast, showLoadingToast } from '../../toast';
import type { Numeric } from '../../utils';
const lang = useCurrentLang();
@ -131,10 +132,12 @@ const customFieldOptions = computed(() => {
});
const loadDynamicOptions = ({ value }: CascaderOption) => {
if (value === '330000') {
if (value === '330000' && asyncState.options![0].children?.length === 0) {
showLoadingToast(t('loading'));
setTimeout(() => {
asyncState.options![0].children = t('asyncOptions2');
}, 500);
closeToast();
}, 1000);
}
};