如何判断网站seo做的好坏,模板创作师,手机网站建设西安,查询网域名解析什么是组合式函数#xff1f; 利用 Vue 的组合式 API 来封装和复用有状态逻辑的函数#xff0c;用于实现逻辑复用#xff0c;类似 react18 中的 hook
函数名称 – 以 use 开头#xff0c;采用驼峰命名#xff0c;如 useTitle参数 – 建议使用 toValue() 处理#xff08;… 什么是组合式函数 利用 Vue 的组合式 API 来封装和复用有状态逻辑的函数用于实现逻辑复用类似 react18 中的 hook
函数名称 – 以 use 开头采用驼峰命名如 useTitle参数 – 建议使用 toValue() 处理以便支持 ref、getter 或普通值的参数返回值 – 建议使用一个包含多个 ref 的普通的非响应式对象以便解构为 ref 之后仍可以保持响应性
相关的工具库 VueUse 组合式函数 vs 公共函数 的区别 公共函数封装无状态的逻辑传入参数后立刻返回期望的返回值如日期格式化函数将指定日期对象格式化为 2024年7月20日23:12:53组合式函数封装有状态逻辑可管理会随时间而变化的状态如将鼠标的坐标声明为响应式变量要想在移动鼠标过程中实时更新鼠标坐标的响应式变量并触发页面渲染则需要使用组合式函数。 响应式的组合式函数 需求随传入的 URL 变化访问接口数据 要点通过侦听器跟踪 URL 的变化
// fetch.js
import { ref, watchEffect, toValue } from vueexport function useFetch(url) {const data ref(null)const error ref(null)const fetchData () {// reset state before fetching..data.value nullerror.value nullfetch(toValue(url)).then((res) res.json()).then((json) (data.value json)).catch((err) (error.value err))}watchEffect(() {fetchData()})return { data, error }
}实战范例 useEventListener 封装添加和清除 DOM 事件监听器的逻辑 import { onMounted, onUnmounted } from vueexport function useEventListener(target, event, callback) {onMounted(() target.addEventListener(event, callback))onUnmounted(() target.removeEventListener(event, callback))
}使用范例 useEventListener(window, mousemove, (event) {x.value event.pageXy.value event.pageY})useTitle https://sunshinehu.blog.csdn.net/article/details/139838689 useLocation https://sunshinehu.blog.csdn.net/article/details/139486731 useMousePosition https://blog.csdn.net/weixin_41192489/article/details/137875217