docs(ImagePreview): use composition api

This commit is contained in:
chenjiahan 2020-12-09 14:48:44 +08:00
parent 9fbda243cf
commit 3d05e56195
3 changed files with 55 additions and 35 deletions

View File

@ -88,22 +88,27 @@ setTimeout(() => {
``` ```
```js ```js
import { reactive, toRefs } from 'vue';
export default { export default {
data() { setup() {
return { const state = reactive({
show: false, show: false,
index: 0, index: 0,
});
const onChange = (index) => {
state.index = index;
};
return {
...toRefs(state),
images: [ images: [
'https://img.yzcdn.cn/vant/apple-1.jpg', 'https://img.yzcdn.cn/vant/apple-1.jpg',
'https://img.yzcdn.cn/vant/apple-2.jpg', 'https://img.yzcdn.cn/vant/apple-2.jpg',
], ],
onChange,
}; };
}, },
methods: {
onChange(index) {
this.index = index;
},
},
}; };
``` ```

View File

@ -123,22 +123,27 @@ setTimeout(() => {
``` ```
```js ```js
import { reactive, toRefs } from 'vue';
export default { export default {
data() { setup() {
return { const state = reactive({
show: false, show: false,
index: 0, index: 0,
});
const onChange = (index) => {
state.index = index;
};
return {
...toRefs(state),
images: [ images: [
'https://img.yzcdn.cn/vant/apple-1.jpg', 'https://img.yzcdn.cn/vant/apple-1.jpg',
'https://img.yzcdn.cn/vant/apple-2.jpg', 'https://img.yzcdn.cn/vant/apple-2.jpg',
], ],
onChange,
}; };
}, },
methods: {
onChange(index) {
this.index = index;
},
},
}; };
``` ```

View File

@ -24,7 +24,7 @@
</demo-block> </demo-block>
<demo-block card :title="t('componentCall')"> <demo-block card :title="t('componentCall')">
<van-cell is-link @click="componentCall"> <van-cell is-link @click="showComponentCall">
{{ t('componentCall') }} {{ t('componentCall') }}
</van-cell> </van-cell>
<van-image-preview v-model:show="show" :images="images" @change="onChange"> <van-image-preview v-model:show="show" :images="images" @change="onChange">
@ -34,7 +34,10 @@
</template> </template>
<script> <script>
import { reactive, toRefs } from 'vue';
import { useTranslate } from '../../composables/use-translate';
import ImagePreview from '..'; import ImagePreview from '..';
import Toast from '../../toast';
const images = [ const images = [
'https://img.yzcdn.cn/vant/apple-1.jpg', 'https://img.yzcdn.cn/vant/apple-1.jpg',
@ -69,36 +72,33 @@ export default {
}, },
}, },
data() { setup() {
return { const t = useTranslate();
const state = reactive({
show: false, show: false,
images,
index: 0, index: 0,
});
const onClose = () => {
Toast(t('closed'));
}; };
},
methods: { const beforeClose = () =>
onClose() { new Promise((resolve) => {
this.$toast(this.t('closed'));
},
beforeClose() {
return new Promise((resolve) => {
setTimeout(() => { setTimeout(() => {
resolve(true); resolve(true);
}, 1000); }, 1000);
}); });
},
componentCall() { const showComponentCall = () => {
this.show = true; state.show = true;
}, };
onChange(index) { const onChange = (index) => {
this.index = index; state.index = index;
}, };
showImagePreview(options = {}) { const showImagePreview = (options = {}) => {
const instance = ImagePreview({ const instance = ImagePreview({
images, images,
...options, ...options,
@ -109,7 +109,17 @@ export default {
instance.close(); instance.close();
}, 2000); }, 2000);
} }
}, };
return {
...toRefs(state),
images,
onClose,
onChange,
beforeClose,
showImagePreview,
showComponentCall,
};
}, },
}; };
</script> </script>