体会1
1、js文件中定义useXX函数
export function usetestY() {
const count = ref(10);
const doubleCount = computed(() => count.value * 2);
return {
count,
doubleCount,
};
}
2、在vue文件中使用useXX函数
import { usetestY } from './data';
const { count, doubleCount } = usetestY();
console.log('1--', count);
console.log('2--', doubleCount);
体会2
3、useAdd自定义hooks文章来源:https://www.toymoban.com/news/detail-727973.html
const useAdd = ({ num1, num2 }) => {
const addNum = ref(0);
watch([num1, num2], ([num1, num2]) => {
addFn(num1, num2);
});
const addFn = (num1, num2) => {
addNum.value = num1 + num2;
};
return {
addNum,
addFn,
};
};
export default useAdd;
4、使用文章来源地址https://www.toymoban.com/news/detail-727973.html
import useAdd from './data';
const { addFn, addNum } = useAdd({ num1, num2 });
addFn(num1.value, num2.value);
console.log('3--', addNum);
到了这里,关于自定义hooks函数的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!