vue单文件组件怎么把逻辑代码抽离为hook

普通的vue单文件组件

log.vue

js 复制代码
<script setup lang="ts">
import { ref } from 'vue';

const index = ref<number>(1)
</script>

<template>
    <h1>{{ index }}</h1>
</template>

<style scoped>
h1 {
    color: red;
    font-size: 50px;
}
</style>

需求

  • 1.把js逻辑代码抽离出去

具体问题链接,前情提要

具体实现

log.hook.ts

js 复制代码
import { ref } from "vue";

export function uselogHook() {
    const index = ref<number>(1);
    
    return {
        index
    }
}

log.vue

js 复制代码
<script setup lang="ts">
import { uselogHook } from "./log.hook";

const { index } = uselogHook()
</script>

<template>
    <h1>{{ index }}</h1>
</template>

<style scoped>
h1 {
    color: red;
    font-size: 50px;
}
</style>

增加需求

  • 1.有一个comoms.ts的文件,其中有一些公共的工具方法暴露出一个类,导入hook在组件中使用

  • 2.props和emits传入hook使用

  • 3.hook中使用生命周期,进行增加监听和移除监听

comoms.ts如下

js 复制代码
export default class Commons {
    constructor() {
    }

    formatTime() {
        console.log('formatTime');
        return new Date().toLocaleString();
    }

}

log.hook.ts

js 复制代码
import { onBeforeUnmount, onMounted, ref } from "vue";
// 导入common.ts
import commonHook from "../common";
export interface Props {
    name: string
}
export interface Emits {
    (e: 'close'): void
}
export function uselogHook(props: Props, emits: Emits) {
    const _props = props
    const _emits = emits
    // 实例化common类
    const common = new commonHook();
    const index = ref<number>(1);
    // 使用传入的参数
    const click = () => {
        console.log(_props.name);
    }

    const keydown = () => {
        console.log("keydown");
        _emits("close");
    }
    onMounted(() => {
        document.addEventListener("keydown", keydown);
    })
    onBeforeUnmount(() => {
        document.removeEventListener("keydown", keydown);
    })

    // 返回变量方法和common实例
    return {
        index, click, keydown, common
    }
}

log.vue

js 复制代码
<script setup lang="ts">
// 导入hook和props,emits类型
import { uselogHook, Props, Emits } from "./log.hook";

// 初始化props,emits
const props = withDefaults(defineProps<Props>(), {
    name: 'default'
})
const emits = defineEmits<Emits>()

// 使用hook传入props,emits  结构出index,click,common
const { index, click, common } = uselogHook(props, emits)
</script>

<template>
    <!-- 显示内容 -->
    <h1 @click="click">{{ index }}</h1>
    <!-- 使用 common公共方法-->
    <h1>{{ common.formatTime() }}</h1>
</template>

<style scoped>
h1 {
    color: red;
    font-size: 50px;
}
</style>
相关推荐
东东5162 分钟前
果园预售系统的设计与实现spingboot+vue
前端·javascript·vue.js·spring boot·个人开发
rainbow68894 分钟前
Python学生管理系统:JSON持久化实战
java·前端·python
打小就很皮...7 分钟前
React Router 7 全局路由保护
前端·react.js·router
起风的蛋挞16 分钟前
Matlab提示词语法
前端·javascript·matlab
有味道的男人17 分钟前
1688获得商品类目调取商品榜单
java·前端·spring
txwtech24 分钟前
第20篇esp32s3小智设置横屏
前端·html
Exquisite.31 分钟前
企业高性能web服务器---Nginx(2)
服务器·前端·nginx
怪兽毕设36 分钟前
基于SpringBoot的选课调查系统
java·vue.js·spring boot·后端·node.js·选课调查系统
DFT计算杂谈38 分钟前
VASP+PHONOPY+pypolymlpj计算不同温度下声子谱,附批处理脚本
java·前端·数据库·人工智能·python
广州华水科技42 分钟前
如何选择合适的单北斗变形监测系统来保障水库安全?
前端