phpjavac / composition Goto Github PK
View Code? Open in Web Editor NEWvue3 compositionApis
Home Page: https://phpjavac.github.io/composition/
vue3 compositionApis
Home Page: https://phpjavac.github.io/composition/
某些函数需要手动清除副作用
eg: setInterval、setTimeout、addEventListener...
这些函数在组件销毁后,需要手动在 destroy或onUnmounted钩子中清除副作用
使用useEffect时,就可以只在一个函数中直接处理这些副作用,而不需要再手动调用组件销毁钩子
eg:
// 需要处理副效应
useEffect(()=>{
const i = setInterval(()=>{
console.log('this is interval, when Unmounted I`m destroy');
}, 2000);
return () => {
clearInterval(i);
}
})
// 不需要处理副效应
useEffect(()=>{
const ii = setInterval(()=>{
console.log('I`m interavl, but always')
}, 2000)
})
新钩子 (之前没有,新加的功能)
参考了React的useEffect方法
return
则表示不需要清除副作用用vue实现该钩子,初版先能够实现第一个关键点: 能够无副作用的执行一个函数
type UseEffectVoid = () => void; // 不需要处理副效应
type UseEffectFunVoid = () => () => void; // 需要处理副效应
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
method | 需要执行的函数(如果需要清除副效应,则在要执行的函数中return一个函数,并在函数中清除副效应) | UseEffectVoid / UseEffectFunVoid | - |
新特性使用场景
新钩子 (之前没有,新加的功能)
esbuild打包
当需要监听键盘,并触发某些代码的时候调用
新钩子 (之前没有,新加的功能)
// 键码
type KeyType = KeyboardEvent['key'] | KeyboardEvent['key'][];
// 触发类型
type keyEvent = 'keydown' | 'keyup' | 'keypress';
// 执行的函数类型
type EventMethod = (event?: KeyboardEvent) => void;
// option选项
interface EventOption {
events?: Array<keyEvent>,
}
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
keyCode | 要监听的key/是否全部监听 | KeyType / true | - |
method | 要执行的方法 | EventMethod | - |
option | 配置项 | EventOption | {event: ['keydown']} |
新功能 (在原有的功能上拓展新功能)
暂时为本地索引,待@vuepress/plugin-docsearch@next 通过后,替换为docsearch🤔
新特性使用场景
新功能 (在原有的功能上拓展新功能)
1
新特性使用场景
跨组件传递消息
新钩子 (之前没有,新加的功能)
const bus = useBus()
or
const { emit , on } = useBus()
bus.emit('',value)
or
emit('',value)
bus.on('',(value)=>void)
or
on('',(value)=>void)
新特性使用场景
新钩子 (之前没有,新加的功能)
添加useAnimation
目前useEffect可以满足
新特性
已用 vue 还原 react 的useEffect方法
新功能 (在原有的功能上拓展新功能)
第二个参数 arr 数组
const refData = ref(1)
const reactiveData = reactive({a: 1})
// 需要监听变量并重新执行函数
useEffect(()=>{
console.log('需要监听变量并重新执行函数', refData.value, reactiveData.a)
}, [refData, reactiveData])
import { useTitle } from 'zcomposition'
const title = useTitle()
console.log(title.value) // print current title
title.value = 'Hello' // change current title
新钩子 (之前没有,新加的功能)
import { useTitle } from 'zcomposition'
const messages = ref(0)
const title = computed(() => {
return !messages.value ? 'No message' : ${messages.value} new messages
})
useTitle(title)
bug描述 不小心删除文件 直接注释该文件的引用
0.0.1 (Default)
Chrome
No response
No response
新特性使用场景
创建ws链接,并自带断线重连
新钩子 (之前没有,新加的功能)
入参:ws配置项
返回值:ws的各种回调(error、message...)
新功能 (在原有的功能上拓展新功能)
无
新特性使用场景
�页面加载时,设置了apiLoading,如果网速快,loading就会很快关闭。
设置一个最小时间,如果要执行的函数 funAsync 的执行时间小于设定时间 time 则等待执行时间满足 time 时,再去执行函数 funThen (当funAsync执行完毕后要执行的函数)
新钩子 (之前没有,新加的功能)
参数
funAsync: () => void, // 想要执行的异步函数 比如api请求
time: number, // 最小时间
funThen: () => void, // 满足最小时间后,要执行的函数 比如设置loading状态
保存时,自动按照eslint格式化代码
新功能 (在原有的功能上拓展新功能)
无
构建失败
0.0.1 (Default)
Firefox, Chrome, Safari, Microsoft Edge
No response
No response
新特性使用场景
元素出现或消失在浏览器视图内触发
新钩子 (之前没有,新加的功能)
参数 targetDom (Ref<HTMLElement | null>)
const finalResult = useObserver(targetDom);
useHover事情触发失败
useAwaitDom模块导入失败
usePermission模块导入失败
useClipboard模块导入失败
useTitle模块导入失败
0.0.1 (Default)
Chrome
No response
实现鼠标聚焦触发事件,失焦触发事件
新钩子 (之前没有,新加的功能)
入参:需要使用的节点dom, 回调函数
回调:聚焦/失焦时触发
异常:可能出现dom挂载问题(解决)
新特性使用场景
等待一个dom或 ref 出现然后执行回调
可用于搭建引导系统
新功能 (在原有的功能上拓展新功能)
interface IUseAwaitDomOptions {
/**
* dom 获取器
*/
domSelector: (() => HTMLElement | null) | Ref<HTMLElement | null>
/**
* 最大等待时间(单位:ms)
*/
maxWait?: number
}
新特性使用场景
一个简单的全局单例仓库
新功能 (在原有的功能上拓展新功能)
const store = useGlobalStore({ count: 0 })
const store2 = useGlobalStore()
store === store2 // true
新特性使用场景[feat]: useDebounceFn & useThrottleFn
新钩子 (之前没有,新加的功能)
[feat]: useDebounceFn & useThrottleFn
新特性使用场景
目前 useAsync
可以更新异步函数的 状态loading
和 数据response
想法 再新增一个可选参数 minTime 最小执行时间
useWaitTime
类似,给异步函数设置一个最小执行时间useWaitTime
相同,控制loading的最小展示时间不算是解决痛点,只是拓展了功能
新功能 (在原有的功能上拓展新功能)
demo 这个例子,只做展示用,所以只处理了loading
export const useWaitAsync = <T>(fun: ()=>Promise<T>, minTime: number) => {
/** 这个函数是为了保证首页不会loading太快 */
const data = ref< T | undefined>();
const loading = ref(true);
const error: {code?: string, message?: string} = {};
let loadingTemp = true;
const promise = () => new Promise<void>((reslove, reject) => {
fun().then((res: T) => {
// 这里不再直接赋值,而是先暂存
data.value = res;
error.code = '0';
error.message = '';
// loading.value = false;
loadingTemp = false;
reslove();
}).catch((err: string) => {
data.value = undefined;
error.code = '999999';
error.message = err;
// loading.value = false;
loadingTemp = false;
reject();
});
});
useWaitTime(() => promise(), minTime, () => {
// 满足最小时间后,再进行赋值,保证最小执行时间
loading.value = loadingTemp;
});
return {
data,
loading,
error,
};
};
新特性使用场景
进入视图/移除视图 需要有回调参数
### 类型
新功能 (在原有的功能上拓展新功能)
### 参数、回调或其他补充
import { useObserver } from 'zcomposition';
const targetDom = ref(null);
const finalResult = useObserver(targetDom,(val)=>{
// 为true则是可视
if(val){
}
},);
新特性使用场景
新功能 (在原有的功能上拓展新功能)
添加 commitlint 支持
新特性使用场景
元素出现或消失在浏览器视图内触发
新钩子 (之前没有,新加的功能)
参数 targetDom (Ref<HTMLElement | null>)
const finalResult = useObserver(targetDom);
useSize切换到其他页面会报错
0.0.1 (Default)
Chrome, Microsoft Edge
v-db21e3ae.b5c0538d.js:1 Uncaught TypeError: Cannot read properties of null (reading 'clientHeight')
at ResizeObserver.<anonymous> (v-db21e3ae.b5c0538d.js:1)
No response
项目初期,有一些配置没有完善,保持一个开放的议题以供相关配置修改的提交能够快速关联到这个议题,防止重复创建相似的议题。
next (未来可能)
新特性使用场景
接受一个异步函数 返回 err,loadging,data
新钩子 (之前没有,新加的功能)
promise回调事件
规范代码风格
新功能 (在原有的功能上拓展新功能)
配置md导入代码文件,修复useEventBus部分问题
新功能 (在原有的功能上拓展新功能)
@[code](@/src/index.ts)
导入代码文件内容
import { useDownloader } from "zcomposition";
替换为
import { useDownloader } from "@lib";
新功能 (在原有的功能上拓展新功能)
无
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.