joken-前端工程师

  :: 首页 :: 博问 :: 闪存 :: 新随笔 :: :: :: 管理 ::

代码

  • 使用方组件
import { defineComponent, PropType, h, computed, ref, watch } from 'vue';

import useMyHooks from './hooks/useMyHooks';

export default defineComponent({
    setup(props, { slots }) {
        const { count, increment, decrement } = useMyHooks();
        return () => {
            return (
                <>
                    <div>
                        <div>{count.value}</div>
                        <button onClick={() => increment()}>more</button>
                        <button onClick={() => decrement()}>less</button>
                    </div>
                </>
            );
        };
    }
});

  • useMyHooks.ts
import { ref, watch } from 'vue';

export default function useMyHooks() {
    const count = ref(0);

    watch(count, (val) => {
        console.log('count changed: ', val);
    });

    return {
        count,
        increment() {
            count.value++;
        },
        decrement() {
            count.value--;
        }
    };
}
posted on 2024-09-13 21:49  joken1310  阅读(18)  评论(0编辑  收藏  举报