docs(Stepper): use composition api

This commit is contained in:
chenjiahan 2020-12-08 21:04:27 +08:00
parent 9af0840f36
commit 7e03486501
3 changed files with 63 additions and 41 deletions

View File

@ -19,11 +19,12 @@ app.use(Stepper);
``` ```
```js ```js
import { ref } from 'vue';
export default { export default {
data() { setup() {
return { const value = ref(1);
value: 1, return { value };
};
}, },
}; };
``` ```
@ -77,24 +78,29 @@ export default {
``` ```
```js ```js
import { ref } from 'vue';
import { Toast } from 'vant'; import { Toast } from 'vant';
export default { export default {
data() { setup() {
return { const value = ref(1);
value: 1,
}; let timer;
}, const onChange = (newValue) => {
methods: { if (newValue === value.value) {
onChange(value) { return;
}
Toast.loading({ forbidClick: true }); Toast.loading({ forbidClick: true });
clearTimeout(this.timer); clearTimeout(this.timer);
this.timer = setTimeout(() => { timer = setTimeout(() => {
Toast.clear(); Toast.clear();
this.value = value; value.value = newValue;
}, 500); }, 500);
}, };
return { value };
}, },
}; };
``` ```

View File

@ -25,11 +25,12 @@ app.use(Stepper);
``` ```
```js ```js
import { ref } from 'vue';
export default { export default {
data() { setup() {
return { const value = ref(1);
value: 1, return { value };
};
}, },
}; };
``` ```
@ -99,25 +100,29 @@ export default {
``` ```
```js ```js
import { ref } from 'vue';
import { Toast } from 'vant'; import { Toast } from 'vant';
export default { export default {
data() { setup() {
return { const value = ref(1);
value: 1,
}; let timer;
}, const onChange = (newValue) => {
methods: { if (newValue === value.value) {
onChange(value) { return;
}
Toast.loading({ forbidClick: true }); Toast.loading({ forbidClick: true });
clearTimeout(this.timer); clearTimeout(this.timer);
this.timer = setTimeout(() => { timer = setTimeout(() => {
Toast.clear(); Toast.clear();
// 注意此时修改 value 后会再次触发 change 事件 value.value = newValue;
this.value = value;
}, 500); }, 500);
}, };
return { value };
}, },
}; };
``` ```

View File

@ -46,6 +46,9 @@
</template> </template>
<script> <script>
import { reactive, toRefs } from 'vue';
import Toast from '../../toast';
export default { export default {
i18n: { i18n: {
'zh-CN': { 'zh-CN': {
@ -70,8 +73,8 @@ export default {
}, },
}, },
data() { setup() {
return { const state = reactive({
stepper1: 1, stepper1: 1,
stepper2: 1, stepper2: 1,
stepper3: 1, stepper3: 1,
@ -82,19 +85,27 @@ export default {
stepper8: 1, stepper8: 1,
stepperRound: 1, stepperRound: 1,
disabledInput: 1, disabledInput: 1,
}; });
},
methods: { let timer;
onChange(value) { const onChange = (newValue) => {
this.$toast.loading({ forbidClick: true }); if (newValue === state.stepper6) {
return;
}
clearTimeout(this.timer); Toast.loading({ forbidClick: true });
this.timer = setTimeout(() => {
this.stepper6 = value; clearTimeout(timer);
this.$toast.clear(); timer = setTimeout(() => {
state.stepper6 = newValue;
Toast.clear();
}, 500); }, 500);
}, };
return {
...toRefs(state),
onChange,
};
}, },
}; };
</script> </script>