自定义暴露方法给外部的hook, hook一定是一个函数,而不是class
// 自定义hook
function useCustomAddCount() {
const state = useState(reactiveState);
useEffect(() => {
setInterval(() => {
state.count++;
}, 1000)
}, () => []);
return state; // 返回整个state反应式对象,而不要只返回其中的某个属性
}
使用
setup() {
this.customCountData = useCustomAddCount();
useImperativeMethods(this.props.ref, {
getData: () => {
console.log('getData');
console.log(this.customCountData.count);
}
})
}
xml使用
<t t-esc="this.customCountData.count"/>