docs(Search): use composition api

This commit is contained in:
chenjiahan 2020-12-06 17:05:55 +08:00
parent e5df5d734e
commit 56047bb655
3 changed files with 53 additions and 38 deletions

View File

@ -19,11 +19,12 @@ app.use(Search);
``` ```
```js ```js
import { ref } from 'vue';
export default { export default {
data() { setup() {
return { const value = ref('');
value: '', return { value };
};
}, },
}; };
``` ```
@ -45,21 +46,23 @@ 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('');
value: '', const onSearch = (val) => {
};
},
methods: {
onSearch(val) {
Toast(val); Toast(val);
}, };
onCancel() { const onCancel = () => {
Toast('Cancel'); Toast('Cancel');
}, };
return {
value,
onSearch,
onCancel,
};
}, },
}; };
``` ```

View File

@ -25,11 +25,12 @@ app.use(Search);
``` ```
```js ```js
import { ref } from 'vue';
export default { export default {
data() { setup() {
return { const value = ref('');
value: '', return { value };
};
}, },
}; };
``` ```
@ -51,21 +52,23 @@ Search 组件提供了 `search` 和 `cancel` 事件,`search` 事件在点击
``` ```
```js ```js
import { ref } from 'vue';
import { Toast } from 'vant'; import { Toast } from 'vant';
export default { export default {
data() { setup() {
return { const value = ref('');
value: '', const onSearch = (val) => {
};
},
methods: {
onSearch(val) {
Toast(val); Toast(val);
}, };
onCancel() { const onCancel = () => {
Toast('取消'); Toast('取消');
}, };
return {
value,
onSearch,
onCancel,
};
}, },
}; };
``` ```

View File

@ -52,6 +52,10 @@
</template> </template>
<script> <script>
import { reactive, toRefs } from 'vue';
import { useTranslate } from '../../composables/use-translate';
import Toast from '../../toast';
export default { export default {
i18n: { i18n: {
'zh-CN': { 'zh-CN': {
@ -74,25 +78,30 @@ export default {
}, },
}, },
data() { setup() {
return { const t = useTranslate();
const state = reactive({
value1: '', value1: '',
value2: '', value2: '',
value3: '', value3: '',
value4: '', value4: '',
value5: '', value5: '',
value6: '', value6: '',
});
const onSearch = (val) => {
Toast(val);
}; };
},
methods: { const onCancel = () => {
onSearch(val) { Toast(t('cancel'));
this.$toast(val); };
},
onCancel() { return {
this.$toast(this.t('cancel')); ...toRefs(state),
}, onSearch,
onCancel,
};
}, },
}; };
</script> </script>