vue3中新增了hooks写法
其目标就是:高内聚低耦合
优点:可以随时被引用,复用
熟悉之后简直快乐到飞起!
下面就是一个简单的例子:
//创建一个hooks.ts
export default const useCounter = (initialValue: number) => {
const count = ref(initialValue);
const increment = () => {
count.value++;
};
const decrement = () => {
count.value--;
};
return {
count,
increment,
decrement,
};
};
//使用
// 引入hooks
import useCounter from './hooks'
const { count, increment, decrement } = useCounter(0);
console.log(count.value); // 0
increment();
console.log(count.value); // 1
decrement();
console.log(count.value); // 0
以上就是一个简单的vue3中hooks的使用
作为初学vue3还需要多多学习!