类别:react / 日期:2023-05-09 / 浏览:544 / 评论:0
如何禁掉 标签默认事件,禁掉之后如何实现跳转。
答:
Link 点击事件 handleClick 部分源码:
if (_this.props.onClick) _this.props.onClick(event);
if (
!event.defaultPrevented && // onClick prevented default
event.button === 0 && // ignore everything but left
!_this.props.target && // let browser handle "target=_blank"
!isModifiedEvent(event) // ignore clicks with modifier keys
) {
event.preventDefault();
var history = _this.context.router.history;
var _this$props = _this.props,
replace = _this$props.replace,
to = _this$props.to;
if (replace) {
history.replace(to);
} else {
history.push(to);
}
}
Link 做了 3 件事情:
有 onclick 那就执行 onclick
click 的时候阻止 a 标签默认事件(这样子点击<a href="/abc">123</a>就不会跳转和刷新页面)
再取得跳转 href(即是 to),用 history(前端路由两种方式之一,history& hash)跳转,此时只是链接变了,并没有刷新页面
版权声明 : 本文未使用任何知识共享协议授权,您可以任何形式自由转载或使用。


发表评论 / 取消回复