Nativescript NativeView捕获异常:返回应用程序后出现TypeError
我的组件\u 1中有一个函数:Nativescript NativeView捕获异常:返回应用程序后出现TypeError,nativescript,Nativescript,我的组件\u 1中有一个函数: methods: { interactBoxToggel () { let page = this.$refs.Home.nativeView; let interactBox = page.getViewById( 'interactBox' ); console.log( interactBox ); } } , mounted () { bus.$on( 'interact
methods: {
interactBoxToggel () {
let page = this.$refs.Home.nativeView;
let interactBox = page.getViewById( 'interactBox' );
console.log( interactBox );
}
} ,
mounted () {
bus.$on( 'interactBoxToggel' , this.interactBoxToggel )
}
如果从组件1内部触发,则它始终工作
现在我将其移植到组件_2:
methods: {
buttonCotrol( buttonName ) {
bus.$emit( 'interactBoxToggel' );
console.log(buttonName);
}
}
它也可以工作,但如果我通过“滑动关闭”(Android 9全屏手势)退出应用程序,然后再次回到应用程序并从内部调用它,我将得到错误:
System.err: An uncaught Exception occurred on "main" thread.
System.err: Calling js method onTouch failed
System.err: TypeError: Cannot read property 'nativeView' of undefined
System.err:
...
为什么会发生这种情况?我如何解决它?每次安装
回调时,都会添加事件侦听器。当应用程序关闭时,该组件将被销毁并在恢复时再次装载,因此您将有2个活动侦听器,但只有一个对会话有效。关闭和打开的次数越多,添加的侦听器就越多
当侦听器被销毁时,必须将其删除
destroyed() {
bus.$off("interactBoxToggel", this.interactBoxToggel);
}
这是上下文,当您从组件_2发出当前页面时,此
将指向当前页面,因此不存在Home ref。当您在另一个页面时,不要更新上一个页面,因为从技术上讲,该页面在UI上不存在。只在导航事件上推送更新。只有一个页面,主页,(主页是Component_1)并且它也包含(Component_2),在关闭应用程序之前,当它从Component_1或Component_2调用时,该功能会工作。请共享一个可以复制问题的最小游乐场示例。我想操纵那个绿色框,点击中间的图标(它们在组件_2[settings.vue]中)非常感谢,在这种情况下,有没有什么方法可以让我引入interactiboxtoggel()而不是on mounted(),这样就不需要删除侦听器了?(我问只是出于好奇)