ExtReact中最大化工具按钮实现报错:Uncaught TypeError: n.up is not a function
解决ExtReact最大化工具按钮的"Uncaught TypeError: n.up is not a function"错误
这个错误的核心原因是你工具按钮handler的参数签名不正确,导致你拿到的panelHdr并不是ExtReact的组件实例,自然没有up()这个Ext组件的内置方法。
问题分析
在ExtReact的组件工具(tool)配置中,handler的默认参数顺序是:(tool, event),而不是你写的(e, target, panelHdr)。你现在把事件对象当成了组件来调用up(),肯定会抛出类型错误。
修正方案
先调整handler的参数顺序,然后通过正确的组件实例来获取父级panel:
{ type: 'maximize', handler: function(tool, event) { // 通过当前工具按钮向上查找panel组件 var panel = tool.up('panel'); if (!panel) return; // 确保找到panel再执行后续逻辑 if (!panel.maximized) { panel.restoreSize = panel.getSize(); panel.restorePos = panel.getPosition(true); panel.maximized = true; var parent = panel.ownerCt; var container = parent ? parent.getTargetEl() : panel.container; // 这里补充你的newBox逻辑... } else { // 记得补充还原的逻辑,避免状态不一致 panel.setSize(panel.restoreSize); panel.setPosition(panel.restorePos); panel.maximized = false; } } }
额外注意点
- 调用
up('panel')后最好做一下空值判断,避免找不到组件时后续代码报错。 - 如果你的panel有特定的xtype或者itemId,也可以用
up('your-custom-xtype')或者up('#your-item-id')来更精准地定位组件。 - 记得维护
maximized状态的切换,同时处理还原逻辑,保证组件状态的一致性。
内容的提问来源于stack exchange,提问作者Милош Рајковић
相关产品推荐
相关产品推荐

