joken-前端工程师

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: :: :: 管理 ::
  404 随笔 :: 39 文章 :: 8 评论 :: 20万 阅读

代码

  • 使用方组件
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   joken1310  阅读(41)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示