我正在尝试使用此组合 API 在 Vue 中构建可重用的模态组件。该计划是公开一些方法,例如toggleModal()调用父组件中的某些事件。我已经在 和 中编写了我的setup()方法methods。
export default {
setup() {
const isModalOpen = ref(false);
const toggleModal = () => {};
return {
toggleModal,
};
},
methods: {
toggleModalMethod() {},
},
};
Run Code Online (Sandbox Code Playgroud)
如果console.log()我的模态组件我可以看到只有我的toggleModalMethod()frommethods被暴露。
有没有办法公开子方法并从父组件调用它?
我正在尝试使用我的React应用程序的lodash库限制滚动事件“滚轮”,但没有成功。
我需要从滚动输入中监听e.deltaY以便检测其滚动方向。为了添加一个侦听器,我编写了一个React钩子,该钩子接受一个事件名和一个处理函数。
基本实施
const [count, setCount] = useState(0);
const handleSections = () => {
setCount(count + 1);
};
const handleWheel = _.throttle(e => {
handleSections();
}, 10000);
useEventListener("wheel", handleWheel);
Run Code Online (Sandbox Code Playgroud)
我的useEventListener挂钩
function useEventListener(e, handler, passive = false) {
useEffect(() => {
window.addEventListener(e, handler, passive);
return function remove() {
window.removeEventListener(e, handler);
};
});
}
Run Code Online (Sandbox Code Playgroud)
工作演示:https : //codesandbox.io/s/throttledemo-hkf7n
我的目标是限制此滚动事件,以减少触发的事件,并有几秒钟的时间以编程方式滚动我的页面(scrollBy(),示例)。目前,节流似乎不起作用,所以我一次收到了很多滚动事件