mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-05-24 23:49:14 +08:00
feat: add usePublicApi
This commit is contained in:
parent
e1ac3f9a58
commit
0d64cf1b63
@ -1,13 +1,6 @@
|
||||
import {
|
||||
ref,
|
||||
watch,
|
||||
computed,
|
||||
reactive,
|
||||
nextTick,
|
||||
onMounted,
|
||||
getCurrentInstance,
|
||||
} from 'vue';
|
||||
import { ref, watch, computed, reactive, nextTick, onMounted } from 'vue';
|
||||
import { createNamespace, pick } from '../utils';
|
||||
import { usePublicApi } from '../composition/use-public-api';
|
||||
import { pickerProps } from '../picker/shared';
|
||||
import Picker from '../picker';
|
||||
|
||||
@ -281,10 +274,7 @@ export default createComponent({
|
||||
}
|
||||
);
|
||||
|
||||
// @exposed-api
|
||||
const vm = getCurrentInstance().proxy;
|
||||
vm.reset = reset;
|
||||
vm.getArea = getArea;
|
||||
usePublicApi({ reset, getArea });
|
||||
|
||||
return () => {
|
||||
const columns = state.columns.slice(0, +props.columnsNum);
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { ref, computed, getCurrentInstance } from 'vue';
|
||||
import { ref, computed } from 'vue';
|
||||
|
||||
// Utils
|
||||
import { createNamespace, addUnit } from '../../utils';
|
||||
@ -18,6 +18,7 @@ import {
|
||||
// Composition
|
||||
import { useHeight } from '../../composition/use-rect';
|
||||
import { useToggle } from '../../composition/use-toggle';
|
||||
import { usePublicApi } from '../composition/use-public-api';
|
||||
|
||||
// Components
|
||||
import Day from './Day';
|
||||
@ -246,13 +247,13 @@ export default createComponent({
|
||||
return <div ref={daysRef} />;
|
||||
};
|
||||
|
||||
// @exposed-api
|
||||
const vm = getCurrentInstance().proxy;
|
||||
vm.height = height;
|
||||
vm.getDate = getDate;
|
||||
vm.getTitle = getTitle;
|
||||
vm.setVisible = setVisible;
|
||||
vm.scrollIntoView = scrollIntoView;
|
||||
usePublicApi({
|
||||
height,
|
||||
getDate,
|
||||
getTitle,
|
||||
setVisible,
|
||||
scrollIntoView,
|
||||
});
|
||||
|
||||
return () => (
|
||||
<div class={bem('month')} ref={monthRef} style={monthStyle.value}>
|
||||
|
7
src/composition/use-public-api.ts
Normal file
7
src/composition/use-public-api.ts
Normal file
@ -0,0 +1,7 @@
|
||||
import { getCurrentInstance } from 'vue';
|
||||
|
||||
// expose public api
|
||||
export function usePublicApi(apis: Record<string, any>) {
|
||||
const vm = (getCurrentInstance() as any).ctx;
|
||||
Object.assign(vm, apis);
|
||||
}
|
@ -5,12 +5,16 @@ import {
|
||||
onActivated,
|
||||
onDeactivated,
|
||||
onBeforeUnmount,
|
||||
getCurrentInstance,
|
||||
} from 'vue';
|
||||
|
||||
// Utils
|
||||
import { createNamespace } from '../utils';
|
||||
import { raf, cancelRaf } from '../utils/dom/raf';
|
||||
import { isSameSecond, parseTimeData, parseFormat } from './utils';
|
||||
|
||||
// Composition
|
||||
import { usePublicApi } from '../composition/use-public-api';
|
||||
|
||||
const [createComponent, bem] = createNamespace('count-down');
|
||||
|
||||
export default createComponent({
|
||||
@ -136,11 +140,11 @@ export default createComponent({
|
||||
|
||||
onBeforeUnmount(pause);
|
||||
|
||||
// @exposed-api
|
||||
const vm = getCurrentInstance().proxy;
|
||||
vm.start = start;
|
||||
vm.reset = reset;
|
||||
vm.pause = pause;
|
||||
usePublicApi({
|
||||
start,
|
||||
reset,
|
||||
pause,
|
||||
});
|
||||
|
||||
return () => (
|
||||
<div class={bem()}>
|
||||
|
@ -1,11 +1,4 @@
|
||||
import {
|
||||
ref,
|
||||
watch,
|
||||
nextTick,
|
||||
onUpdated,
|
||||
onMounted,
|
||||
getCurrentInstance,
|
||||
} from 'vue';
|
||||
import { ref, watch, nextTick, onUpdated, onMounted } from 'vue';
|
||||
|
||||
// Utils
|
||||
import { createNamespace } from '../utils';
|
||||
@ -14,6 +7,7 @@ import { isHidden } from '../utils/dom/style';
|
||||
// Composition
|
||||
import { useRect } from '../composition/use-rect';
|
||||
import { useScroller } from '../composition/use-scroller';
|
||||
import { usePublicApi } from '../composition/use-public-api';
|
||||
import { useGlobalEvent } from '../composition/use-global-event';
|
||||
|
||||
// Components
|
||||
@ -147,12 +141,9 @@ export default createComponent({
|
||||
}
|
||||
});
|
||||
|
||||
usePublicApi({ check });
|
||||
useGlobalEvent(scroller, 'scroll', check);
|
||||
|
||||
// @exposed-api
|
||||
const vm = getCurrentInstance().proxy;
|
||||
vm.check = check;
|
||||
|
||||
return () => {
|
||||
const Content = slots.default?.();
|
||||
const Placeholder = (
|
||||
|
Loading…
x
Reference in New Issue
Block a user